module Rails::Surrender::ModelFilterScopes::ClassMethods
Public Instance Methods
apply_surrender_column_datetime_scopes(child)
click to toggle source
# File lib/rails/surrender/model_filter_scopes.rb, line 39 def apply_surrender_column_datetime_scopes(child) with_surrender_datetime_columns(child) do |column| base = column.split(/_at$/).first scope "filter_by_#{base}_to".to_sym, ->(time) { where("#{child.table_name}.#{column} <= ?", time) } scope "filter_by_#{base}_from".to_sym, ->(time) { where("#{child.table_name}.#{column} >= ?", time) } scope "filter_by_#{base}_before".to_sym, ->(time) { where("#{child.table_name}.#{column} < ?", time) } scope "filter_by_#{base}_after".to_sym, ->(time) { where("#{child.table_name}.#{column} > ?", time) } end end
apply_surrender_column_name_scopes(child)
click to toggle source
# File lib/rails/surrender/model_filter_scopes.rb, line 33 def apply_surrender_column_name_scopes(child) child.column_names.each do |column| scope "filter_by_#{column}".to_sym, ->(val) { where({ column.to_sym => val }) } end end
inherited(child)
click to toggle source
Calls superclass method
# File lib/rails/surrender/model_filter_scopes.rb, line 13 def inherited(child) super # Bad things happen if you ask table_exists? to ApplicationRecord while it's loading! # TODO: Add configuration in case ApplicationRecord is _not_ the primary abstract class return if child.name == 'ApplicationRecord' return unless child.table_exists? child.instance_eval do # scope to filter by every column name apply_surrender_column_name_scopes(child) # scope to filter by date or time column names apply_surrender_column_datetime_scopes(child) rescue StandardError # TODO: why are tests failing here!!! end end
with_surrender_datetime_columns(child, &block)
click to toggle source
# File lib/rails/surrender/model_filter_scopes.rb, line 50 def with_surrender_datetime_columns(child, &block) child.columns.select { |c| c.type.in? %i[date datetime] }.map(&:name).each(&block) end