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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/detour/acts_as_flaggable.rb', line 12

def acts_as_flaggable(options = {})
  Detour::Feature.class_eval "has_one :\#{table_name}_percentage_flag,\nclass_name: \"Detour::PercentageFlag\",\ninverse_of: :feature,\ndependent:  :destroy,\nconditions: { flaggable_type: \"\#{self}\" }\n\nset_human_attribute_name(:\"\#{table_name}_percentage_flag.percentage\", \"Percentage\")\nattr_accessible :\#{table_name}_percentage_flag_attributes\n\naccepts_nested_attributes_for :\#{table_name}_percentage_flag,\nupdate_only: true,\nreject_if: proc { |attrs| attrs[:percentage].blank? }\n\nhas_many :\#{table_name}_defined_group_flags,\nclass_name: \"Detour::DefinedGroupFlag\",\ninverse_of: :feature,\ndependent: :destroy,\nconditions: { flaggable_type: \"\#{self}\" }\n\nattr_accessible :\#{table_name}_defined_group_flags_attributes\naccepts_nested_attributes_for :\#{table_name}_defined_group_flags, allow_destroy: true\n\nhas_many :\#{table_name}_database_group_flags,\nclass_name: \"Detour::DatabaseGroupFlag\",\ninverse_of: :feature,\ndependent: :destroy,\nconditions: { flaggable_type: \"\#{self}\" }\n\nattr_accessible :\#{table_name}_database_group_flags_attributes\naccepts_nested_attributes_for :\#{table_name}_database_group_flags, allow_destroy: true\n\nhas_many :\#{table_name}_flag_ins,\nclass_name: \"Detour::FlagInFlag\",\ninverse_of: :feature,\ndependent:  :destroy,\nconditions: { flaggable_type: \"\#{self}\" }\n\nset_human_attribute_name(:\"\#{table_name}_flag_ins.\#{self.model_name}\", \"\#{self.model_name}\")\nattr_accessible :\#{table_name}_flag_ins_attributes\naccepts_nested_attributes_for :\#{table_name}_flag_ins, allow_destroy: true\n\nhas_many :\#{table_name}_opt_outs,\nclass_name: \"Detour::OptOutFlag\",\ninverse_of: :feature,\ndependent:  :destroy,\nconditions: { flaggable_type: \"\#{self}\" }\n\nset_human_attribute_name(:\"\#{table_name}_opt_outs.\#{self.model_name}\", \"\#{self.model_name}\")\nattr_accessible :\#{table_name}_opt_outs_attributes\naccepts_nested_attributes_for :\#{table_name}_opt_outs, allow_destroy: true\n"

  Detour::Group.class_eval "set_human_attribute_name(:\"memberships.\#{self.model_name}\", \"\#{self.model_name}\")\n"

  class_eval do
    @detour_flaggable_find_by = :id

    has_many :flag_in_flags,
      as: :flaggable,
      class_name: "Detour::FlagInFlag"

    has_many :memberships,
      as: :member,
      class_name: "Detour::Membership"

    has_many :groups,
      through: :memberships,
      class_name: "Detour::Group"

    has_many :database_group_flags,
      through: :groups,
      class_name: "Detour::DatabaseGroupFlag"

    has_many :opt_out_flags,
      as: :flaggable,
      class_name: "Detour::OptOutFlag"

    if options[:find_by]
      @detour_flaggable_find_by = options[:find_by]
    end

    def defined_group_flags
      @defined_group_flags ||= Detour::DefinedGroupFlag.where(flaggable_type: self.class.to_s)
    end

    def percentage_flags
      @percentage_flags ||= Detour::PercentageFlag
        .where(flaggable_type: self.class.to_s)
        .where("? % 10 < detour_flags.percentage / 10", id)
    end

    def self.detour_flaggable_find_by
      @detour_flaggable_find_by
    end

    include Detour::Flaggable
  end
end