Module: Rails::Surrender::ModelFilterScopes::ClassMethods
- Defined in:
- lib/rails/surrender/model_filter_scopes.rb
Instance Method Summary collapse
- #apply_surrender_column_datetime_scopes(child) ⇒ Object
- #apply_surrender_column_name_scopes(child) ⇒ Object
- #inherited(child) ⇒ Object
- #with_surrender_datetime_columns(child, &block) ⇒ Object
Instance Method Details
#apply_surrender_column_datetime_scopes(child) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# 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) ⇒ Object
33 34 35 36 37 |
# 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) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# 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) ⇒ Object
50 51 52 |
# 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 |