Module: ROM::Plugins::Relation::SQL::AutoRestrictions
- Defined in:
- lib/rom/plugins/relation/sql/auto_restrictions.rb
Overview
Generates methods for restricting relations by their indexed attributes
This plugin must be enabled for the whole adapter, ‘use` won’t work as schema is not yet available, unless it was defined explicitly.
Constant Summary collapse
- EmptySchemaError =
Class.new(ArgumentError) do def initialize(klass) super("#{klass} relation has no schema. " \ "Make sure :auto_restrictions is enabled after defining a schema") end end
Class Method Summary collapse
Class Method Details
.included(klass) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/rom/plugins/relation/sql/auto_restrictions.rb', line 36 def self.included(klass) super schema = klass.schema raise EmptySchemaError, klass if schema.nil? methods, mod = restriction_methods(schema) klass.include(mod) methods.each { |meth| klass.auto_curry(meth) } end |
.restriction_methods(schema) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rom/plugins/relation/sql/auto_restrictions.rb', line 45 def self.restriction_methods(schema) mod = Module.new indexed_attrs = schema.select { |attr| attr.[:index] } methods = indexed_attrs.map do |attr| meth_name = :"by_#{attr.name}" mod.module_eval do define_method(meth_name) do |value| where(attr.is(value)) end end meth_name end [methods, mod] end |