Module: Hobo::Model::Permissions::ClassMethods

Defined in:
lib/hobo/model/permissions.rb

Instance Method Summary collapse

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 belongs_to_with_hobo_permission_check(association_id, *args, &extension)
  belongs_to_without_hobo_permission_check(association_id, *args, &extension)
  reflection = reflections[association_id.to_s]
  if reflection.options[: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 has_many_with_hobo_permission_check(association_id, *args, &extension)
  has_many_without_hobo_permission_check(association_id, *args, &extension)
  reflection = reflections[association_id.to_s]
  if reflection.options[: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 has_one_with_hobo_permission_check(association_id, *args, &extension)
  has_one_without_hobo_permission_check(association_id, *args, &extension)
  reflection = reflections[association_id.to_s]
  if reflection.options[: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

Yields:

  • (record)


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

Returns:



75
76
77
# File 'lib/hobo/model/permissions.rb', line 75

def viewable_by?(user, attribute=nil)
  new.viewable_by?(user, attribute)
end