Class: JsonbAccessor::AttributeQueryMethods
- Inherits:
-
Object
- Object
- JsonbAccessor::AttributeQueryMethods
- Defined in:
- lib/jsonb_accessor/attribute_query_methods.rb
Instance Method Summary collapse
- #define(store_key_mapping_method_name, jsonb_attribute) ⇒ Object
-
#initialize(klass) ⇒ AttributeQueryMethods
constructor
A new instance of AttributeQueryMethods.
Constructor Details
#initialize(klass) ⇒ AttributeQueryMethods
Returns a new instance of AttributeQueryMethods.
5 6 7 |
# File 'lib/jsonb_accessor/attribute_query_methods.rb', line 5 def initialize(klass) @klass = klass end |
Instance Method Details
#define(store_key_mapping_method_name, jsonb_attribute) ⇒ Object
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 |
# File 'lib/jsonb_accessor/attribute_query_methods.rb', line 9 def define(store_key_mapping_method_name, jsonb_attribute) return if klass.superclass.respond_to? store_key_mapping_method_name # <jsonb_attribute>_where scope klass.define_singleton_method "#{jsonb_attribute}_where" do |attributes| store_key_attributes = JsonbAccessor::Helpers.convert_keys_to_store_keys(attributes, all.model.public_send(store_key_mapping_method_name)) jsonb_where(jsonb_attribute, store_key_attributes) end # <jsonb_attribute>_where_not scope klass.define_singleton_method "#{jsonb_attribute}_where_not" do |attributes| store_key_attributes = JsonbAccessor::Helpers.convert_keys_to_store_keys(attributes, all.model.public_send(store_key_mapping_method_name)) jsonb_where_not(jsonb_attribute, store_key_attributes) end # <jsonb_attribute>_order scope klass.define_singleton_method "#{jsonb_attribute}_order" do |*args| = args. order_by_defaults = args.each_with_object({}) { |attribute, config| config[attribute] = :asc } store_key_mapping = all.model.public_send(store_key_mapping_method_name) order_by_defaults.merge().reduce(all) do |query, (name, direction)| key = store_key_mapping[name.to_s] order_query = jsonb_order(jsonb_attribute, key, direction) query.merge(order_query) end end end |