Module: ErpTechSvcs::Extensions::ActiveRecord::ScopedBy::ClassMethods

Defined in:
lib/erp_tech_svcs/extensions/active_record/scoped_by.rb

Instance Method Summary collapse

Instance Method Details

#add_scoped_by(attr_name = :scoped_by) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/erp_tech_svcs/extensions/active_record/scoped_by.rb', line 19

def add_scoped_by(attr_name=:scoped_by)
  # serialize Scope attributes
  is_json attr_name

  extend SingletonMethods
  include InstanceMethods

  # create method to retrieve scoped_by attribute name
  define_singleton_method("retrieve_scoped_by_name") do
    attr_name
  end

  # create method to initialize the json field with an empty hash
  define_method("initialize_#{attr_name}_scoped_by_json") do
    if self.new_record?
      send("#{attr_name}=", {})
    end
  end
  after_initialize "initialize_#{attr_name}_scoped_by_json"
end