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) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/erp_tech_svcs/extensions/active_record/scoped_by.rb', line 15

def add_scoped_by(attr_name)
  # serialize Scope attributes
  is_json attr_name

  extend SingletonMethods
  include InstanceMethods

  # 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