Module: FlexibleFeeds::PolymorphicJoin::ClassMethods

Defined in:
lib/flexible_feeds/polymorphic_join.rb

Instance Method Summary collapse

Instance Method Details

#define_add(join_table, association, singular_association) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/flexible_feeds/polymorphic_join.rb', line 29

def define_add(join_table, association, singular_association)
  define_method "add_#{singular_association}".to_sym do |instance|
    result = send(join_table).create(singular_association => instance)
    self.reload()
    result
  end
end

#define_association(join_table, association, singular_association) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/flexible_feeds/polymorphic_join.rb', line 15

def define_association(join_table, association, singular_association)
  define_method association do
    send(join_table).includes(singular_association).collect do |join|
      join.send(singular_association)
    end
  end
end

#define_include(join_table, association, singular_association) ⇒ Object



23
24
25
26
27
# File 'lib/flexible_feeds/polymorphic_join.rb', line 23

def define_include(join_table, association, singular_association)
  define_method "#{association}_include?".to_sym do |instance|
    send(join_table).where(singular_association => instance).exists?
  end
end

#define_remove(join_table, association, singular_association) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/flexible_feeds/polymorphic_join.rb', line 37

def define_remove(join_table, association, singular_association)
  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

#polymorphically_joined_through(join_table, params) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/flexible_feeds/polymorphic_join.rb', line 6

def polymorphically_joined_through(join_table, params)
  association = params[:association_name]
  singular_association = params[:singular_association_name]
  define_association(join_table, association, singular_association)
  define_include(join_table, association, singular_association)
  define_add(join_table, association, singular_association)
  define_remove(join_table, association, singular_association)
end