Class: Detour::FlagForm

Inherits:
Object
  • Object
show all
Defined in:
lib/detour/flag_form.rb

Instance Method Summary collapse

Constructor Details

#initialize(flaggable_type) ⇒ FlagForm

Returns a new instance of FlagForm.



2
3
4
# File 'lib/detour/flag_form.rb', line 2

def initialize(flaggable_type)
  @flaggable_type = flaggable_type
end

Instance Method Details

#errors?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/detour/flag_form.rb', line 10

def errors?
  features.any? { |feature| feature.errors.any? }
end

#featuresObject



6
7
8
# File 'lib/detour/flag_form.rb', line 6

def features
  @features ||= Detour::Feature.includes("#{@flaggable_type}_percentage_flag", "#{@flaggable_type}_group_flags").with_lines
end

#group_flags_for(feature, initialize = true) ⇒ Object



22
23
24
25
26
27
# File 'lib/detour/flag_form.rb', line 22

def group_flags_for(feature, initialize = true)
  group_names.map do |group_name|
    flags = feature.send("#{@flaggable_type}_group_flags")
    flags.detect { |flag| flag.group_name == group_name } || (flags.new(group_name: group_name) if initialize)
  end
end

#group_namesObject



14
15
16
17
18
19
20
# File 'lib/detour/flag_form.rb', line 14

def group_names
  @group_names ||= begin
    all_names = features.collect { |feature| feature.send("#{@flaggable_type}_group_flags").collect(&:group_name) }.uniq.flatten
    defined_group_names = Detour.config.defined_groups.fetch(@flaggable_type.classify, {}).keys.map(&:to_s)
    (all_names | defined_group_names).sort
  end
end

#update_attributes(params) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/detour/flag_form.rb', line 29

def update_attributes(params)
  Detour::Feature.transaction do |transaction|
    features.map do |feature|
      feature_params = params[:features][feature.name]
      next unless feature_params

      check_percentage_flag_for_deletion(feature, feature_params)
      set_group_flag_params(feature, feature_params)

      feature.assign_attributes feature_params
      feature.save if feature.changed_for_autosave?
    end

    if features.any? { |feature| feature.errors.any? }
      raise ActiveRecord::Rollback
    else
      true
    end
  end
end