app/models/unidom/common/concerns/model_extension.rb in unidom-common-0.5.1 vs app/models/unidom/common/concerns/model_extension.rb in unidom-common-0.6

- old
+ new

@@ -19,10 +19,15 @@ scope :valid_duration, ->(range) { where "(#{includer.table_name}.opened_at BETWEEN ? AND ?) OR (#{includer.table_name}.closed_at <= ? AND #{includer.table_name}.closed_at >= ?)", range.min, range.max, range.max, range.min } scope :alive, ->(living = true) { where defunct: !living } scope :dead, ->(defunct = true) { where defunct: defunct } + scope :notation_column_where, ->(name, operator, value) do + operation = :like==operator ? { operator: 'ILIKE', value: "%#{value}%" } : { operator: operator.to_s, value: value } + where "#{table_name}.notation -> 'columns' ->> '#{name}' #{operation[:operator]} :value", value: operation[:value] + end + if columns_hash['ordinal'].present? validates :ordinal, presence: true, numericality: { integer_only: true, greater_than: 0 } scope :ordinal_is, ->(ordinal) { where ordinal: ordinal } end @@ -84,11 +89,15 @@ end if name.ends_with? '_at' matched = /\A(.+)_at\z/.match name class_eval do - scope :"#{matched[1]}_on", ->(date) { where name => date.beginning_of_day..date.end_of_day } - scope :"#{matched[1]}_during", ->(range) { where name => range } + scope :"#{matched[1]}_on", ->(date) { where name => date.beginning_of_day..date.end_of_day } + scope :"#{matched[1]}_during", ->(range) { where name => range } + scope :"#{matched[1]}_before", ->(time) { where "#{table_name}.#{name} < :time", time: time } + scope :"#{matched[1]}_not_after", ->(time) { where "#{table_name}.#{name} <= :time", time: time } + scope :"#{matched[1]}_after", ->(time) { where "#{table_name}.#{name} > :time", time: time } + scope :"#{matched[1]}_not_before", ->(time) { where "#{table_name}.#{name} >= :time", time: time } end end end \ No newline at end of file