allow children if a boolean field is set to 0

24 views
Skip to first unread message

Quiliro Ordóñez

unread,
Nov 17, 2011, 7:03:33 PM11/17/11
to hobo...@googlegroups.com
Hi folks.

I have an app where I have to be able to add records to a table if the
parent record has a boolean field value of 0. Otherwise it should be
impossible to add new records that belong_to that record.

kevinpfromnm

unread,
Nov 18, 2011, 4:01:40 PM11/18/11
to hobo...@googlegroups.com
start with the permissions on the child model.  create_permitted? should check that the parent is set and the value is appropriate.

Then you probably want the new/create action to be an owned action instead of standard so that the parent is properly set.

Aside: I prefer to think of booleans in true/false.  DB storage varies, in ruby truth is not nil or false.  Anything else is true

Quiliro Ordóñez

unread,
Nov 21, 2011, 1:58:15 PM11/21/11
to hobo...@googlegroups.com
Thank you very much, Kevin. Please help me with my questions below.


On 18/11/11 16:01, kevinpfromnm wrote:
start with the permissions on the child model.  create_permitted? should check that the parent is set and the value is appropriate.

I have been trying to understand how to do this because I'm just starting to understand the swign of Hobo. Here is my shot. Please confirm:



class OneAccount < ActiveRecord::Base

  hobo_model # Don't put anything above this

  fields do
    name         :string
#    balance       :integer, :default => 0
    transactional :boolean, :default => false
    timestamps
  end

  belongs_to :zero_account
  has_many :two_accounts, :dependent => :destroy, :accessible => true
  children :two_accounts

  # --- Permissions --- #
  def create_permitted?
    zero_account.transactional?
  end
  def update_permitted?
    acting_user.administrator?
  end
  def destroy_permitted?
    acting_user.administrator?
  end
  def view_permitted?(field)
    true
  end

end


class ZeroAccount < ActiveRecord::Base

  hobo_model # Don't put anything above this

  fields do
    name          :string
#    balance       :integer, :default => 0
    transactional :boolean, :default => false
    timestamps
  end
 
  has_many :one_accounts, :dependent => :destroy, :accessible => true
  children :one_accounts

  # --- Permissions --- #
  def create_permitted?
    acting_user.administrator?
  end
  def update_permitted?
    acting_user.administrator?
  end
  def destroy_permitted?
    acting_user.administrator?
  end
  def view_permitted?(field)
    true
  end

end




Then you probably want the new/create action to be an owned action instead of standard so that the parent is properly set.


class ZeroAccountsController < ApplicationController

  hobo_model_controller

  auto_actions :all
  auto_actions_for :one_acount, [:new, :create]

end





Aside: I prefer to think of booleans in true/false.  DB storage varies, in ruby truth is not nil or false.  Anything else is true

Is my reasoning along those guidelines?

Quiliro Ordóñez

unread,
Nov 21, 2011, 2:25:59 PM11/21/11
to hobo...@googlegroups.com
This should change to:

def create_permitted?
   zero_account.transactional?

  end
  def update_permitted?
    acting_user.administrator?
  end
  def destroy_permitted?
    acting_user.administrator?
  end
  def view_permitted?(field)
    true
  end

end


Then you probably want the new/create action to be an owned action instead of standard so that the parent is properly set.


class ZeroAccountsController < ApplicationController

  hobo_model_controller

  auto_actions :all
  auto_actions_for :one_acount, [:new, :create]

end




Aside: I prefer to think of booleans in true/false.  DB storage varies, in ruby truth is not nil or false.  Anything else is true

Is my reasoning along those guidelines?
--
You received this message because you are subscribed to the Google Groups "Hobo Users" group.
To post to this group, send email to hobo...@googlegroups.com.
To unsubscribe from this group, send email to hobousers+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/hobousers?hl=en.

kevinpfromnm

unread,
Nov 21, 2011, 3:41:07 PM11/21/11
to hobo...@googlegroups.com
Mostly.  You probably want to remove the new/create actions from the standard auto actions as it doesn't make sense to create this model without that association being set.

Quiliro Ordóñez

unread,
Nov 21, 2011, 3:57:55 PM11/21/11
to hobo...@googlegroups.com

This is what I have now. Is this a bad setup?

class OneAccount < ActiveRecord::Base

hobo_model # Don't put anything above this

fields do
name :string
# balance :integer, :default => 0
transactional :boolean, :default => false
timestamps
end

belongs_to :zero_account
has_many :two_accounts, :dependent => :destroy, :accessible => true
children :two_accounts

# --- Permissions --- #
def create_permitted?

!zero_account.transactional?

end

class ZeroAccount < ActiveRecord::Base

end

class OneAccountsController < ApplicationController

hobo_model_controller

auto_actions :all, :except => :index
auto_actions_for :zero_account, [:new, :create]

end

class ZeroAccountsController < ApplicationController

hobo_model_controller

auto_actions :all

end

The problem is that zero_account.transactional must not be checked if I
want inline new child create_new. Your input is thankfully received.

kevinpfromnm

unread,
Nov 21, 2011, 5:15:05 PM11/21/11
to hobo...@googlegroups.com
It looks fine but what I was saying, by having auto_actions :all you include new and create actions that won't have that particular association set.  So probably use:

auto_actions :all, :except => [:index, :new, :create]

instead.

Quiliro Ordóñez

unread,
Nov 21, 2011, 6:06:44 PM11/21/11
to hobo...@googlegroups.com
On the OneAccountsController or in the TwoAccountsController?

Quiliro Ordóñez

unread,
Nov 21, 2011, 6:13:51 PM11/21/11
to hobo...@googlegroups.com
Sorry. I meant : "On the OneAccountsController or in the ZeroAccountsController?

kevinpfromnm

unread,
Nov 22, 2011, 3:38:41 PM11/22/11
to hobo...@googlegroups.com
On the oneaccountscontroller.  The one that has the auto_actions_for defined.

Quiliro Ordóñez

unread,
Nov 28, 2011, 8:40:03 AM11/28/11
to hobo...@googlegroups.com
On 22/11/11 15:38, kevinpfromnm wrote:
> On the oneaccountscontroller. The one that has the auto_actions_for
> defined.

Thanks. I will upload the app and share it with an AGLP v3 license.

Reply all
Reply to author
Forward
0 new messages