Sha256: 4e9f6742d7becb7fa07ebabb38201f58d847ce514622cbe988eee177258029d4

Contents?: true

Size: 1.81 KB

Versions: 26

Compression:

Stored size: 1.81 KB

Contents

module AnafHabtm
  module ActiveRecord
    def anaf_habtm(association, options={}, &block)
      class_eval do
        # Define a proc that will look up the (potentially) existing object
        finder = proc {|id| association.to_s.singularize.camelize.constantize.where(:id=>id).first
        }

        # Define a proc that will set the association collection
        set_collection = proc {|me, coll| me.send("#{association.to_s.tableize}=", coll)}
        has_and_belongs_to_many association.to_sym, options
        # Define the actual association setter.
        define_method "#{association.to_s.tableize}_attributes=", lambda{|attributes_for_association|
          coll = []

          attributes_for_association.each_value do |params|
            next if params["_destroy"] == "1"
            obj = finder.call(params["id"]) if params.has_key?("id")
            params.extend(AnafHabtm::HashExtension)
            # ActiveRecord::Base.attributes=() doesn't like extra parameters.
            coll << block.call(params.copy_without_destroy, obj)
          end
          set_collection.call(self, coll)
        }
      end
    end

    def named_association(member, attribute, opts={})
      member = member.to_s
      klass = opts.has_key?(:class) ? opts[:class_name] : member.to_s.singularize.camelize
      attribute = attribute.to_s
      if opts.has_key?(:create)
        class_eval "def #{member}_#{attribute}=(#{attribute});
        return if #{attribute}.blank?
        self.#{member} = #{klass}.find_or_create_by_#{attribute}(#{attribute})
        end;"
      else
        class_eval "def #{member}_#{attribute}=(#{attribute}); self.#{member} = #{klass}.find_by_#{attribute}(#{attribute}) unless #{attribute}.blank?; end;"
      end
      class_eval "def #{member}_#{attribute}; #{member}.#{attribute} if #{member}; end;"
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
casey_jones-0.1.2 lib/anaf_active_record.rb
casey_jones-0.1.1 lib/anaf_active_record.rb
casey_jones-0.1.0 lib/anaf_active_record.rb
casey_jones-0.0.121 lib/anaf_active_record.rb
casey_jones-0.0.120 lib/anaf_active_record.rb
casey_jones-0.0.119 lib/anaf_active_record.rb
casey_jones-0.0.118 lib/anaf_active_record.rb
casey_jones-0.0.117 lib/anaf_active_record.rb
casey_jones-0.0.116 lib/anaf_active_record.rb
casey_jones-0.0.115 lib/anaf_active_record.rb
casey_jones-0.0.114 lib/anaf_active_record.rb
casey_jones-0.0.113 lib/anaf_active_record.rb
casey_jones-0.0.112 lib/anaf_active_record.rb
casey_jones-0.0.111 lib/anaf_active_record.rb
casey_jones-0.0.110 lib/anaf_active_record.rb
casey_jones-0.0.109 lib/anaf_active_record.rb
casey_jones-0.0.108 lib/anaf_active_record.rb
casey_jones-0.0.107 lib/anaf_active_record.rb
casey_jones-0.0.106 lib/anaf_active_record.rb
casey_jones-0.0.105 lib/anaf_active_record.rb