Sha256: 41960f80c76d1c7e290ba5f7f42091cddfd9bdc072da90ba29a6a522c66adff8

Contents?: true

Size: 999 Bytes

Versions: 3

Compression:

Stored size: 999 Bytes

Contents

module ActsInRelation
  module Roles
    class Source < Base
      def define
        actions.each do |action|
          @class.class_eval <<-RUBY
            has_many :"#{action.pluralize}",
              foreign_key: :"#{source}_id",
              dependent: :destroy

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

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

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

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

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

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
acts_in_relation-0.2.2 lib/acts_in_relation/roles/source.rb
acts_in_relation-0.2.1 lib/acts_in_relation/roles/source.rb
acts_in_relation-0.2.0 lib/acts_in_relation/roles/source.rb