module AutoDefineScope::ActiveRecordExtension

Constants

OPERATE_SQL

Attributes

auto_define_scopes[RW]

Public Instance Methods

add_scope(scope_name, lmd) click to toggle source
# File lib/auto_define_scope/active_record_extension.rb, line 22
def add_scope scope_name, lmd
  scope scope_name, lmd
  @auto_define_scopes ||= []
  @auto_define_scopes << scope_name
end
scopes(options = { search: [], with: [] }) click to toggle source
# File lib/auto_define_scope/active_record_extension.rb, line 16
def scopes options = { search: [], with: [] }
  search_columns options[:search] if options[:search]
  with_columns options[:with] if options[:with]
  filterrific available_filters: @auto_define_scopes if @auto_define_scopes.present?
end
search_columns(*columns) click to toggle source

For example, CustomerAgentHistory include this columns is a array like [“status”, “p_way”, {customer_agent: [“code”, “name”, agent: :province_id]}]

# File lib/auto_define_scope/active_record_extension.rb, line 8
def search_columns(*columns)
  generate_cope columns, :search
end
with_columns(*columns) click to toggle source
# File lib/auto_define_scope/active_record_extension.rb, line 12
def with_columns(*columns)
  generate_cope columns, :with
end

Protected Instance Methods

generate_cope(columns, type, klass = self, joins_params_array = []) click to toggle source
# File lib/auto_define_scope/active_record_extension.rb, line 29
def generate_cope(columns, type, klass = self, joins_params_array = [])
  columns.each do |c|
    if c.is_a?(Hash)
      c.keys.each do |k|
        association = klass.reflect_on_all_associations.detect{ |a| a.name == k.to_sym }
        raise "scopes defined error, not found association('#{k}') for model #{klass.name}" if association.nil?
        _klass = association.class_name.constantize
        value = c[k].is_a?(Array) ? c[k] : [c[k]] 
        generate_cope value, type, _klass, joins_params_array + [k]
      end
      next
    end
    if c.is_a? Array
      c.each do |v|
        value = v.is_a?(Array) ? v : [v] 
        generate_cope value, type, klass, joins_params_array
      end
      next
    end
    column_type = klass.columns_hash[c.to_s]&.type
    raise "scopes defined error, not found column('#{c}') for table #{klass.table_name}" if column_type.nil?
    joins_params = joins_params_array.reverse.inject() { |a, n| { n => a } }
    table_name = klass.table_name
    @auto_define_scopes ||= []
    scope_name = "#{type}_#{joins_params_array.last&.to_s&.+ '_'}#{c}"
    @auto_define_scopes << scope_name
    scope scope_name, ->(v) do
      value = case column_type
              when :integer
                v.to_i
              when :string
                v.to_s
              else
                v
              end
      value = type == :with ? value : "%#{value}%"
      joins(joins_params).where("#{table_name}.#{c} #{OPERATE_SQL[type]}", value) 
    end
  end
  nil
end