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.classify.constantize
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_collection}_percentage_flag", "#{flaggable_collection}_database_group_flags", "#{flaggable_collection}_defined_group_flags").with_lines
end

#group_flags_for(feature, types = %w[defined database]) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/detour/flag_form.rb', line 18

def group_flags_for(feature, types = %w[defined database])
  Array.wrap(types).inject([]) do |flags, type|
    flags.concat _group_flags_for(feature, type)
  end.sort_by do |flag|
    flag.group.name.downcase
  end
end

#groupsObject



14
15
16
# File 'lib/detour/flag_form.rb', line 14

def groups
  @groups ||= (database_groups + defined_groups).sort_by { |group| group.name.downcase }
end

#update_attributes(params) ⇒ Object



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

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)
      process_group_flags(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