Sha256: e87786e76fad0fe6c2d60a2ecb5d69ae5b25e9439b578aed2bebbd2e18a866d7

Contents?: true

Size: 975 Bytes

Versions: 3

Compression:

Stored size: 975 Bytes

Contents

module ActsInRelation
  module Source
    def define
      with.each do |action|
        action.extend ActsInRelation::Supports::Verb

        class_object.class_eval <<-RUBY
          has_many :"#{action.pluralize}",
            foreign_key: :"#{source}_id",
            dependent: :destroy

          has_many :"#{action.progressize}",
            through: :"#{action.pluralize}",
            source: :"target_#{target}"

          def #{action}(target)
            return if #{action.progressize}?(target) || (self == target)

            #{action.pluralize}.create(target_#{target}_id: target.id)
          end

          def un#{action}(target)
            return unless #{action.progressize}?(target)

            #{action.pluralize}.find_by(target_#{target}_id: target.id).destroy
          end

          def #{action.progressize}?(target)
            #{action.pluralize}.exists?(target_#{target}_id: target.id)
          end
        RUBY
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
acts_in_relation-0.1.1 lib/acts_in_relation/source.rb
acts_in_relation-0.1.0 lib/acts_in_relation/source.rb
acts_in_relation-0.0.1 lib/acts_in_relation/source.rb