Sha256: 2877d6866f6f49961f2b4815cf21913664e22f4413fb860d334b946759c9310b

Contents?: true

Size: 1.79 KB

Versions: 5

Compression:

Stored size: 1.79 KB

Contents

module AuthHelpers
  module Model

    # Checks for a column that ends with _id in the included model. Then it adds
    # a belongs_to association, accepts_nested_attributes_for and make the nested
    # attributes accessible.
    #
    # Also includes a hook called remove_association_error, that removes the nested
    # attribute errors from the parent object.
    #
    # Finally, if the *_id in the table has also *_type. It considers a polymorphic
    # association.
    #
    # Whenever using this method with polymorphic association, don't forget to
    # set the validation scope in AuthLogic.
    #
    #   a.validations_scope = :accountable_type
    #
    module Associatable
      def self.included(base)
        column = base.columns.detect{|c| c.name =~ /_id$/ }
        raise ScriptError, "Could not find a column that ends with id in #{base.name.tableize}" unless column

        association = column.name.gsub(/_id$/, '').to_sym
        polymorphic = !!base.columns.detect{ |c| c.name == "#{association}_type" }

        base.class_eval do
          belongs_to association, :validate => true, :dependent => :destroy,
                                  :autosave => true, :polymorphic => polymorphic

          accepts_nested_attributes_for association
          attr_accessible :"#{association}_attributes"

          after_validation :remove_association_error
        end

        base.class_eval <<-ASSOCIATION
          # Remove association errors from the message
          #
          def remove_association_error
            self.errors.each do |key, value|
              next unless key.to_s =~ /^#{association}_/
              self.errors.instance_variable_get('@errors').delete(key)
            end
          end
          protected :remove_association_error
        ASSOCIATION
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
josevalim-auth_helpers-0.2.0 lib/auth_helpers/model/associatable.rb
josevalim-auth_helpers-0.2.1 lib/auth_helpers/model/associatable.rb
josevalim-auth_helpers-0.3.0 lib/auth_helpers/model/associatable.rb
josevalim-auth_helpers-0.3.1 lib/auth_helpers/model/associatable.rb
josevalim-auth_helpers-0.3.2 lib/auth_helpers/model/associatable.rb