Class: Mobility::Backend::Sequel::Jsonb::QueryMethods
- Inherits:
-
QueryMethods
- Object
- Module
- QueryMethods
- Mobility::Backend::Sequel::Jsonb::QueryMethods
- Defined in:
- lib/mobility/backend/sequel/jsonb/query_methods.rb
Instance Method Summary collapse
-
#initialize(attributes, **options) ⇒ QueryMethods
constructor
A new instance of QueryMethods.
Constructor Details
#initialize(attributes, **options) ⇒ QueryMethods
Returns a new instance of QueryMethods.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/mobility/backend/sequel/jsonb/query_methods.rb', line 6 def initialize(attributes, **) super attributes_extractor = @attributes_extractor define_method :_filter_or_exclude do |invert, clause, *conds, &block| if (clause == :where) && i18n_keys = attributes_extractor.call(conds.first) locale = Mobility.locale.to_s table_name = model.table_name cond = conds.first i18n_query = i18n_keys.inject(::Sequel.expr(!invert)) do |expr, attr| value = cond.delete(attr) attr_jsonb = ::Sequel.pg_jsonb_op(attr) contains_value = attr_jsonb.contains({ locale => value }) has_key = attr_jsonb.has_key?(locale) if invert expr.|(has_key & ~contains_value) else expr.&(value.nil? ? ~has_key : contains_value) end end super(invert, clause, *conds, &block).where(i18n_query) else super(invert, clause, *conds, &block) end end attributes.each do |attribute| define_method :"first_by_#{attribute}" do |value| where(::Sequel.pg_jsonb_op(attribute).contains({ Mobility.locale => value })). select_all(model.table_name).first end end end |