Module: Hobo::Model::Permissions::ClassMethods
- Defined in:
- lib/hobo/model/permissions.rb
Instance Method Summary collapse
- #belongs_to_with_hobo_permission_check(association_id, *args, &extension) ⇒ Object
-
#has_many_with_hobo_permission_check(association_id, *args, &extension) ⇒ Object
ensure active_user gets passed down to :dependent => destroy associations (Ticket #528).
- #has_one_with_hobo_permission_check(association_id, *args, &extension) ⇒ Object
- #user_create(user, attributes = {}, &block) ⇒ Object
- #user_create!(user, attributes = {}, &block) ⇒ Object
- #user_find(user, *args) {|record| ... } ⇒ Object
- #user_new(user, attributes = {}) ⇒ Object
- #viewable_by?(user, attribute = nil) ⇒ Boolean
Instance Method Details
#belongs_to_with_hobo_permission_check(association_id, *args, &extension) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/hobo/model/permissions.rb', line 109 def (association_id, *args, &extension) (association_id, *args, &extension) reflection = reflections[association_id.to_s] if reflection.[:dependent]==:destroy #overriding dynamic method created in ActiveRecord::Associations#configure_dependency_for_belongs_to method_name = "belongs_to_dependent_destroy_for_#{reflection.name}".to_sym define_method(method_name) do association = send(reflection.name) unless association.nil? association.is_a?(Hobo::Model) ? association.user_destroy(acting_user) : association.destroy end end end end |
#has_many_with_hobo_permission_check(association_id, *args, &extension) ⇒ Object
ensure active_user gets passed down to :dependent => destroy
associations (Ticket #528)
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/hobo/model/permissions.rb', line 82 def (association_id, *args, &extension) (association_id, *args, &extension) reflection = reflections[association_id.to_s] if reflection.[:dependent]==:destroy #overriding dynamic method created in ActiveRecord::Associations#configure_dependency_for_has_many method_name = "has_many_dependent_destroy_for_#{reflection.name}".to_sym define_method(method_name) do send(reflection.name).each { |r| r.is_a?(Hobo::Model) ? r.user_destroy(acting_user) : r.destroy } end end end |
#has_one_with_hobo_permission_check(association_id, *args, &extension) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/hobo/model/permissions.rb', line 94 def (association_id, *args, &extension) (association_id, *args, &extension) reflection = reflections[association_id.to_s] if reflection.[:dependent]==:destroy #overriding dynamic method created in ActiveRecord::Associations#configure_dependency_for_has_one method_name = "has_one_dependent_destroy_for_#{reflection.name}".to_sym define_method(method_name) do association = send(reflection.name) unless association.nil? association.is_a?(Hobo::Model) ? association.user_destroy(acting_user) : association.destroy end end end end |
#user_create(user, attributes = {}, &block) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/hobo/model/permissions.rb', line 54 def user_create(user, attributes={}, &block) if attributes.is_a?(Array) attributes.map { |attrs| user_create(user, attrs) } else record = user_new(user, attributes, &block) record.user_save(user) record end end |
#user_create!(user, attributes = {}, &block) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/hobo/model/permissions.rb', line 65 def user_create!(user, attributes={}, &block) if attributes.is_a?(Array) attributes.map { |attrs| user_create(user, attrs) } else record = user_new(user, attributes, &block) record.user_save!(user) record end end |
#user_find(user, *args) {|record| ... } ⇒ Object
36 37 38 39 40 41 |
# File 'lib/hobo/model/permissions.rb', line 36 def user_find(user, *args) record = find(*args) yield(record) if block_given? record.user_view user record end |
#user_new(user, attributes = {}) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/hobo/model/permissions.rb', line 44 def user_new(user, attributes={}) new(attributes) do |r| r.set_creator user yield r if block_given? r.user_view(user) r.with_acting_user(user) { r.try.after_user_new } end end |
#viewable_by?(user, attribute = nil) ⇒ Boolean
75 76 77 |
# File 'lib/hobo/model/permissions.rb', line 75 def viewable_by?(user, attribute=nil) new.viewable_by?(user, attribute) end |