Module: Detour::ActsAsFlaggable

Defined in:
lib/detour/acts_as_flaggable.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_flaggable(options = {}) ⇒ Object

Sets up ActiveRecord associations for the including class, and includes Flaggable in the class.

Examples:

class User < ActiveRecord::Base
  acts_as_taggable find_by: :email
end

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :find_by (Symbol)

    The field to find the record by when running rake tasks. Defaults to :id.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/detour/acts_as_flaggable.rb', line 12

def acts_as_flaggable(options = {})
  Detour::Feature.class_eval "    has_one :\#{table_name}_percentage_flag,\n      class_name: \"Detour::PercentageFlag\",\n      inverse_of: :feature,\n      dependent:  :destroy,\n      conditions: { flaggable_type: \"\#{self}\" }\n\n    attr_accessible :\#{table_name}_percentage_flag_attributes\n\n    accepts_nested_attributes_for :\#{table_name}_percentage_flag,\n      update_only: true,\n      reject_if: proc { |attrs| attrs[:percentage].blank? }\n\n    has_many :\#{table_name}_defined_group_flags,\n      class_name: \"Detour::DefinedGroupFlag\",\n      inverse_of: :feature,\n      dependent: :destroy,\n      conditions: { flaggable_type: \"\#{self}\" }\n\n    attr_accessible :\#{table_name}_defined_group_flags_attributes\n    accepts_nested_attributes_for :\#{table_name}_defined_group_flags, allow_destroy: true\n\n    has_many :\#{table_name}_database_group_flags,\n      class_name: \"Detour::DatabaseGroupFlag\",\n      inverse_of: :feature,\n      dependent: :destroy,\n      conditions: { flaggable_type: \"\#{self}\" }\n\n    attr_accessible :\#{table_name}_database_group_flags_attributes\n    accepts_nested_attributes_for :\#{table_name}_database_group_flags, allow_destroy: true\n\n    has_many :\#{table_name}_flag_ins,\n      class_name: \"Detour::FlagInFlag\",\n      inverse_of: :feature,\n      dependent:  :destroy,\n      conditions: { flaggable_type: \"\#{self}\" }\n\n    attr_accessible :\#{table_name}_flag_ins_attributes\n    accepts_nested_attributes_for :\#{table_name}_flag_ins, allow_destroy: true\n\n    has_many :\#{table_name}_opt_outs,\n      class_name: \"Detour::OptOutFlag\",\n      inverse_of: :feature,\n      dependent:  :destroy,\n      conditions: { flaggable_type: \"\#{self}\" }\n\n    attr_accessible :\#{table_name}_opt_outs_attributes\n    accepts_nested_attributes_for :\#{table_name}_opt_outs, allow_destroy: true\n  EOF\n\n  class_eval do\n    @detour_flaggable_find_by = :id\n\n    has_many :flag_in_flags,\n      as: :flaggable,\n      class_name: \"Detour::FlagInFlag\"\n\n    has_many :opt_out_flags,\n      as: :flaggable,\n      class_name: \"Detour::OptOutFlag\"\n\n    if options[:find_by]\n      @detour_flaggable_find_by = options[:find_by]\n    end\n\n    def self.detour_flaggable_find_by\n      @detour_flaggable_find_by\n    end\n\n    include Detour::Flaggable\n  end\nend\n"