Module: Embedded::Model
- Defined in:
- lib/embedded/model.rb
Constant Summary collapse
- ScopeMethod =
ActiveRecord::VERSION::MAJOR >= 4 ? :all.freeze : :scoped.freeze
Instance Method Summary collapse
- #embedded ⇒ Object
- #embedded_attributes ⇒ Object
- #embedded_column_names(embeddable_attr, attributes) ⇒ Object
- #embeds(embeddable_attr, options = {}) ⇒ Object
Instance Method Details
#embedded ⇒ Object
11 12 13 |
# File 'lib/embedded/model.rb', line 11 def Embedded::Scope.new(send(ScopeMethod),) end |
#embedded_attributes ⇒ Object
15 16 17 |
# File 'lib/embedded/model.rb', line 15 def @embedded_attributes ||= {} end |
#embedded_column_names(embeddable_attr, attributes) ⇒ Object
5 6 7 8 9 |
# File 'lib/embedded/model.rb', line 5 def (, attributes) attributes.inject({}) do |hash, a| hash.merge(:"#{}_#{a}" => a) end end |
#embeds(embeddable_attr, options = {}) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/embedded/model.rb', line 19 def (, = {}) self.[] = attributes = [:attrs] columns = (,attributes) clazz = [:class_name] ? [:class_name].constantize : .to_s.camelcase.constantize self.send(:define_method, ) do values = columns.inject({}) do |hash,(k,v)| hash.merge(v=>read_attribute(k)) end clazz.new(values) end self.send(:define_method, :"#{}=") do |v| columns.each do |k,a| write_attribute(k, v.send(a)) end end end |