Sha256: 81a5a5ba852ef5549e3765cf9f50629b1909ed06ecdb893e1a0e21bc02a42c9c

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

module ErpTechSvcs
  module Extensions
    module ActiveRecord
      module ScopedBy

        module Errors
        end

        def self.included(base)
          base.extend(ClassMethods)
        end

        def _config
          @config
        end

        module ClassMethods

          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

        end

        module SingletonMethods
          def scoped_by(scope_name, scope_value)
            where(arel_table[retrieve_scoped_by_name].matches("%\"#{scope_name}\":\"#{scope_value}\"%").or(arel_table[retrieve_scoped_by_name].matches("%\"#{scope_name}\":#{scope_value}%")))
          end
        end

        module InstanceMethods
          def add_scope(scope_name, scope_value)
            send(self.class.retrieve_scoped_by_name)[scope_name.to_s] = scope_value
            save!
          end

          def remove_scope(scope_name)
            send(self.class.retrieve_scoped_by_name)[scope_name.to_s] = nil
            save!
          end

          def get_scope(scope_name)
            send(self.class.retrieve_scoped_by_name)[scope_name.to_s]
          end
        end

      end # ScopedBy
    end # ActiveRecord
  end # Extensions
end # ErpTechSvcs

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
erp_tech_svcs-4.2.0 lib/erp_tech_svcs/extensions/active_record/scoped_by.rb