Module: Detour::ActsAsFlaggable
- Defined in:
- lib/detour/acts_as_flaggable.rb
Instance Method Summary collapse
-
#acts_as_flaggable(options = {}) ⇒ Object
Sets up ActiveRecord associations for the including class, and includes Flaggable in the class.
Instance Method Details
#acts_as_flaggable(options = {}) ⇒ Object
Sets up ActiveRecord associations for the including class, and includes Flaggable in the class.
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 |
# File 'lib/detour/acts_as_flaggable.rb', line 12 def acts_as_flaggable( = {}) Detour::Feature.class_eval <<-EOF has_one :#{table_name}_percentage_flag, class_name: "Detour::PercentageFlag", inverse_of: :feature, dependent: :destroy, conditions: { flaggable_type: "#{self}" } attr_accessible :#{table_name}_percentage_flag_attributes accepts_nested_attributes_for :#{table_name}_percentage_flag, update_only: true, reject_if: proc { |attrs| attrs[:percentage].blank? } has_many :#{table_name}_defined_group_flags, class_name: "Detour::DefinedGroupFlag", inverse_of: :feature, dependent: :destroy, conditions: { flaggable_type: "#{self}" } attr_accessible :#{table_name}_defined_group_flags_attributes accepts_nested_attributes_for :#{table_name}_defined_group_flags, allow_destroy: true has_many :#{table_name}_database_group_flags, class_name: "Detour::DatabaseGroupFlag", inverse_of: :feature, dependent: :destroy, conditions: { flaggable_type: "#{self}" } attr_accessible :#{table_name}_database_group_flags_attributes accepts_nested_attributes_for :#{table_name}_database_group_flags, allow_destroy: true has_many :#{table_name}_flag_ins, class_name: "Detour::FlagInFlag", inverse_of: :feature, dependent: :destroy, conditions: { flaggable_type: "#{self}" } attr_accessible :#{table_name}_flag_ins_attributes accepts_nested_attributes_for :#{table_name}_flag_ins, allow_destroy: true has_many :#{table_name}_opt_outs, class_name: "Detour::OptOutFlag", inverse_of: :feature, dependent: :destroy, conditions: { flaggable_type: "#{self}" } attr_accessible :#{table_name}_opt_outs_attributes accepts_nested_attributes_for :#{table_name}_opt_outs, allow_destroy: true EOF 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 [:find_by] @detour_flaggable_find_by = [: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 |