module Hobo::Model::Permissions::ClassMethods

Public Instance Methods

belongs_to_with_hobo_permission_check(association_id, *args, &extension) click to toggle source
# 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) click to toggle source

ensure active_user gets passed down to :dependent => destroy associations (Ticket #528)

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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| ... } click to toggle source
# 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={}) { |r| ... } click to toggle source
# 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) click to toggle source
# File lib/hobo/model/permissions.rb, line 75
def viewable_by?(user, attribute=nil)
  new.viewable_by?(user, attribute)
end