Class: ActsInRelation::Roles::Source

Inherits:
Base
  • Object
show all
Defined in:
lib/acts_in_relation/roles/source.rb

Constant Summary

Constants included from Supports::Verb

Supports::Verb::PATCHES

Instance Method Summary collapse

Methods inherited from Base

#actions, #initialize, #source, #target

Methods included from Supports::Verb

#pastize, #peoplize, #progressize

Constructor Details

This class inherits a constructor from ActsInRelation::Roles::Base

Instance Method Details

#defineObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/acts_in_relation/roles/source.rb', line 4

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