Sha256: 1a54cef254e638e66e99608af0a5e4964c2466e7ff016f0d8cbb1f8be490109b

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

module FlexibleFeeds
  module PolymorphicJoin
    extend ActiveSupport::Concern

    module ClassMethods
      def polymorphically_joined_through(join_table, params)
        association = params[:association_name]
        singular_association = params[:singular_association_name]
        
        define_method association do
          send(join_table).collect { |join| join.send(singular_association) }
        end
        
        define_method "#{association}_include?".to_sym do |instance|
          send(join_table).where(singular_association => instance).exists?
        end
        
        define_method "add_#{singular_association}".to_sym do |instance|
          result = send(join_table).create(singular_association => instance)
          self.reload()
          result
        end
        
        define_method "remove_#{singular_association}".to_sym do |instance|
          return false unless send("#{association}_include?", instance)
          result = send(join_table).
            find_by(singular_association => instance).destroy
          self.reload()
          result
        end
      end
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
flexible_feeds-0.1.0 lib/flexible_feeds/polymorphic_join.rb