Skip to content

Commit

Permalink
moved Nested Attributes writer generation in separate module, with sa…
Browse files Browse the repository at this point in the history
…me API as ActiveRecord
  • Loading branch information
rvalyi committed Apr 25, 2014
1 parent 56fd74e commit a9e5257
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 21 deletions.
1 change: 1 addition & 0 deletions lib/ooor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Ooor
autoload :Base
autoload :ModelSchema
autoload :Persistence
autoload :NestedAttributes
autoload :Callbacks
autoload :Cache, 'active_support/cache'
autoload :Serialization
Expand Down
2 changes: 1 addition & 1 deletion lib/ooor/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Ooor
# the base class for proxies to OpenERP objects
class Base < Ooor::MiniActiveResource
include Naming, TypeCasting, Serialization, ReflectionOoor, Reflection
include Associations, Report, FinderMethods, FieldMethods
include Associations, Report, FinderMethods, FieldMethods, NestedAttributes

# ********************** class methods ************************************
class << self
Expand Down
18 changes: 1 addition & 17 deletions lib/ooor/field_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,12 @@ def fast_fields(options={})
end
end

# this is used by fields_for in ActionView FormHelper
def define_nested_attributes_method(meth)
unless self.respond_to?(meth)
self.instance_eval do
define_method "#{meth}_attributes=" do |*args|
send("#{meth}_will_change!")
@associations[meth] = args[0]
@loaded_associations[meth] = args[0]
end
define_method "#{meth}_attributes" do |*args|
@loaded_associations[meth]
end
end
end
end

private

def generate_accessors #TODO we should cache this is a module cached like the template, or eventually generate source code or both
fields.keys.each { |meth| define_field_method meth }
associations_keys.each { |meth| define_association_method meth }
one2many_associations.keys.each { |meth| define_nested_attributes_method meth }
one2many_associations.keys.each { |meth| accepts_nested_attributes_for meth } #TODO do it for m2o too
many2one_associations.keys.each do |meth|
define_association_method meth
define_m2o_association_method meth
Expand Down
48 changes: 48 additions & 0 deletions lib/ooor/nested_attributes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'active_support/concern'

module Ooor
module NestedAttributes #:nodoc:
extend ActiveSupport::Concern

module ClassMethods

# Defines an attributes writer for the specified association(s).
# Note that in Ooor this is active by default for all one2many and many2one associations
def accepts_nested_attributes_for(*attr_names)
attr_names.each do |association_name|
# reflection = all_fields[association_name]
generate_association_writer(association_name, :collection) #TODO add support for m2o
end
end

private

# Generates a writer method for this association. Serves as a point for
# accessing the objects in the association. For example, this method
# could generate the following:
#
# def pirate_attributes=(attributes)
# assign_nested_attributes_for_one_to_one_association(:pirate, attributes)
# end
#
# This redirects the attempts to write objects in an association through
# the helper methods defined below. Makes it seem like the nested
# associations are just regular associations.
def generate_association_writer(association_name, type)
unless self.respond_to?(association_name)
self.instance_eval do
define_method "#{association_name}_attributes=" do |*args|
send("#{association_name}_will_change!")
@associations[association_name] = args[0]
@loaded_associations[association_name] = args[0]
end
define_method "#{association_name}_attributes" do |*args|
@loaded_associations[association_name]
end
end
end
end

end
end
end
3 changes: 0 additions & 3 deletions lib/ooor/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,6 @@ def set_model_template!(klass, options)
template = Ooor.model_registry.get_template(config, options[:model])
if template
klass.t = template
klass.one2many_associations.keys.each do |meth|
klass.define_nested_attributes_method(meth)
end
else
template = Ooor::ModelSchema.new
template.openerp_model = options[:model]
Expand Down

0 comments on commit a9e5257

Please sign in to comment.