Class: EgovUtils::ModelPermissions
- Inherits:
 - 
      Object
      
        
- Object
 - EgovUtils::ModelPermissions
 
 
- Defined in:
 - lib/egov_utils/model_permissions.rb
 
Instance Attribute Summary collapse
- 
  
    
      #ability  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    
Returns the value of attribute ability.
 - 
  
    
      #model  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    
Returns the value of attribute model.
 
Class Method Summary collapse
Instance Method Summary collapse
- #add_editable_association_attributes(attributes, assoc, action) ⇒ Object
 - #add_editable_association_attributes_for(attributes, assoc, entity, action) ⇒ Object
 - #assignable_associations ⇒ Object
 - #basic_editable_attributes(action = :update) ⇒ Object
 - #basic_editable_attributes_for(entity, action = :update) ⇒ Object
 - #editable_attributes(action = :update) ⇒ Object
 - 
  
    
      #editable_attributes_for(entity, action = :update)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
TODO nested attributes should take entity as well - what to do with has_many?.
 - 
  
    
      #initialize(model, ability)  ⇒ ModelPermissions 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    
A new instance of ModelPermissions.
 - #readable_attributes(action = :show) ⇒ Object
 
Constructor Details
#initialize(model, ability) ⇒ ModelPermissions
Returns a new instance of ModelPermissions.
      20 21 22  | 
    
      # File 'lib/egov_utils/model_permissions.rb', line 20 def initialize(model, ability) @model, @ability = model, ability end  | 
  
Instance Attribute Details
#ability ⇒ Object (readonly)
Returns the value of attribute ability.
      18 19 20  | 
    
      # File 'lib/egov_utils/model_permissions.rb', line 18 def ability @ability end  | 
  
#model ⇒ Object (readonly)
Returns the value of attribute model.
      18 19 20  | 
    
      # File 'lib/egov_utils/model_permissions.rb', line 18 def model @model end  | 
  
Class Method Details
.build(model, ability) ⇒ Object
      4 5 6 7 8 9 10 11 12 13 14 15 16  | 
    
      # File 'lib/egov_utils/model_permissions.rb', line 4 def self.build(model, ability) klass = model klasses = [klass] while klass != klass.base_class klass = klass.superclass klasses << klass end klasses.each do |kls| perm_class = "#{kls.name}Permissions".safe_constantize return perm_class.new(ability) if perm_class end EgovUtils::ModelPermissions.new(model, ability) end  | 
  
Instance Method Details
#add_editable_association_attributes(attributes, assoc, action) ⇒ Object
      55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75  | 
    
      # File 'lib/egov_utils/model_permissions.rb', line 55 def add_editable_association_attributes(attributes, assoc, action) = self.class.build(assoc.klass, ability) assoc_attributes = .editable_attributes(action) assoc_attributes.unshift('id') if ability.can?(:update, assoc.klass) assoc_attributes_hash = { "#{assoc.name}_attributes" => assoc_attributes } case assoc.macro when :has_many attributes << assoc_attributes_hash when :has_one, :belongs_to if ability.can?(:update, assoc.klass) attributes << assoc.foreign_key if assoc.macro == :belongs_to attributes << assoc_attributes_hash else attributes << assoc_attributes_hash end else attributes << assoc_attributes_hash end end  | 
  
#add_editable_association_attributes_for(attributes, assoc, entity, action) ⇒ Object
      77 78 79 80 81 82 83 84  | 
    
      # File 'lib/egov_utils/model_permissions.rb', line 77 def add_editable_association_attributes_for(attributes, assoc, entity, action) case assoc.macro when :has_many else add_editable_association_attributes(attributes, assoc, action) end end  | 
  
#assignable_associations ⇒ Object
      36 37 38  | 
    
      # File 'lib/egov_utils/model_permissions.rb', line 36 def assignable_associations model.reflect_on_all_associations.select{|assoc| model.method_defined?("#{assoc.name}_attributes=".to_sym) } end  | 
  
#basic_editable_attributes(action = :update) ⇒ Object
      28 29 30  | 
    
      # File 'lib/egov_utils/model_permissions.rb', line 28 def basic_editable_attributes(action=:update) model.column_names - ['id', 'updated_at', 'created_at'] end  | 
  
#basic_editable_attributes_for(entity, action = :update) ⇒ Object
      32 33 34  | 
    
      # File 'lib/egov_utils/model_permissions.rb', line 32 def basic_editable_attributes_for(entity, action=:update) basic_editable_attributes(action) end  | 
  
#editable_attributes(action = :update) ⇒ Object
      40 41 42 43 44 45  | 
    
      # File 'lib/egov_utils/model_permissions.rb', line 40 def editable_attributes(action=:update) return [] unless ability.can?(action, model) attributes = basic_editable_attributes(action) assignable_associations.each{|assoc| add_editable_association_attributes(attributes, assoc, action) } attributes end  | 
  
#editable_attributes_for(entity, action = :update) ⇒ Object
TODO nested attributes should take entity as well - what to do with has_many?
      48 49 50 51 52 53  | 
    
      # File 'lib/egov_utils/model_permissions.rb', line 48 def editable_attributes_for(entity, action=:update) return [] unless ability.can?(action, entity) attributes = basic_editable_attributes(action) assignable_associations.each{|assoc| add_editable_association_attributes_for(attributes, assoc, entity, action) } attributes end  | 
  
#readable_attributes(action = :show) ⇒ Object
      24 25 26  | 
    
      # File 'lib/egov_utils/model_permissions.rb', line 24 def readable_attributes(action=:show) model.column_names end  |