Class: Magick::AdminUI::FeaturesController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/magick/adminui/features_controller.rb

Instance Method Summary collapse

Instance Method Details

#available_rolesObject



19
20
21
# File 'app/controllers/magick/adminui/features_controller.rb', line 19

def available_roles
  Magick::AdminUI.config.available_roles || []
end

#available_tagsObject



23
24
25
# File 'app/controllers/magick/adminui/features_controller.rb', line 23

def available_tags
  Magick::AdminUI.config.tags
end

#disableObject



87
88
89
90
# File 'app/controllers/magick/adminui/features_controller.rb', line 87

def disable
  @feature.disable
  redirect_to magick_admin_ui.features_path, notice: 'Feature disabled'
end

#disable_for_roleObject



107
108
109
110
111
112
113
114
115
# File 'app/controllers/magick/adminui/features_controller.rb', line 107

def disable_for_role
  role = params[:role]
  if role.present?
    @feature.disable_for_role(role)
    redirect_to magick_admin_ui.feature_path(@feature.name), notice: "Feature disabled for role: #{role}"
  else
    redirect_to magick_admin_ui.feature_path(@feature.name), alert: 'Role is required'
  end
end

#editObject



57
58
# File 'app/controllers/magick/adminui/features_controller.rb', line 57

def edit
end

#enableObject



82
83
84
85
# File 'app/controllers/magick/adminui/features_controller.rb', line 82

def enable
  @feature.enable
  redirect_to magick_admin_ui.features_path, notice: 'Feature enabled'
end

#enable_for_roleObject



97
98
99
100
101
102
103
104
105
# File 'app/controllers/magick/adminui/features_controller.rb', line 97

def enable_for_role
  role = params[:role]
  if role.present?
    @feature.enable_for_role(role)
    redirect_to magick_admin_ui.feature_path(@feature.name), notice: "Feature enabled for role: #{role}"
  else
    redirect_to magick_admin_ui.feature_path(@feature.name), alert: 'Role is required'
  end
end

#enable_for_userObject



92
93
94
95
# File 'app/controllers/magick/adminui/features_controller.rb', line 92

def enable_for_user
  @feature.enable_for_user(params[:user_id])
  redirect_to magick_admin_ui.feature_path(@feature.name), notice: 'Feature enabled for user'
end

#indexObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/magick/adminui/features_controller.rb', line 32

def index
  @features = Magick.features.values

  # Filter by group if provided
  if params[:group].present?
    @features = @features.select { |f| f.group == params[:group] }
  end

  # Filter by search query (name or description)
  if params[:search].present?
    search_term = params[:search].downcase
    @features = @features.select do |f|
      f.name.downcase.include?(search_term) ||
      (f.display_name && f.display_name.downcase.include?(search_term)) ||
      (f.description && f.description.downcase.include?(search_term))
    end
  end

  # Get all available groups for filter dropdown
  @available_groups = Magick.features.values.map(&:group).compact.uniq.sort
end

#magick_admin_uiObject



15
16
17
# File 'app/controllers/magick/adminui/features_controller.rb', line 15

def magick_admin_ui
  Magick::AdminUI::Engine.routes.url_helpers
end

#partially_enabled?(feature) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
# File 'app/controllers/magick/adminui/features_controller.rb', line 27

def partially_enabled?(feature)
  targeting = feature.instance_variable_get(:@targeting) || {}
  targeting.any? && !targeting.empty?
end

#showObject



54
55
# File 'app/controllers/magick/adminui/features_controller.rb', line 54

def show
end

#updateObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/magick/adminui/features_controller.rb', line 60

def update
  # Update group if provided
  if params.key?(:group)
    @feature.set_group(params[:group])
  end

  if @feature.type == :boolean
    # For boolean features, checkbox sends 'true' when checked, nothing when unchecked
    # Rails form helpers handle this - if checkbox is unchecked, params[:value] will be nil
    value = params[:value] == 'true'
    @feature.set_value(value)
  elsif params[:value].present?
    # For string/number features, convert to appropriate type
    value = params[:value]
    if @feature.type == :number
      value = value.include?('.') ? value.to_f : value.to_i
    end
    @feature.set_value(value)
  end
  redirect_to magick_admin_ui.feature_path(@feature.name), notice: 'Feature updated successfully'
end

#update_targetingObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'app/controllers/magick/adminui/features_controller.rb', line 117

def update_targeting
  # Handle targeting updates from form
  targeting_params = params[:targeting] || {}

  # Ensure we're using the registered feature instance
  feature_name = @feature.name.to_s
  @feature = Magick.features[feature_name] if Magick.features.key?(feature_name)

  current_targeting = @feature.instance_variable_get(:@targeting) || {}

  # Handle roles - always clear existing and set new ones
  # Rails checkboxes don't send unchecked values, so we need to check what was sent
  current_roles = current_targeting[:role].is_a?(Array) ? current_targeting[:role] : (current_targeting[:role] ? [current_targeting[:role]] : [])
  selected_roles = Array(targeting_params[:roles]).reject(&:blank?)

  # Disable roles that are no longer selected
  (current_roles - selected_roles).each do |role|
    @feature.disable_for_role(role) if role.present?
  end

  # Enable newly selected roles
  (selected_roles - current_roles).each do |role|
    @feature.enable_for_role(role) if role.present?
  end

  # Handle tags - always clear existing and set new ones
  # Rails checkboxes don't send unchecked values, so we need to check what was sent
  current_tags = current_targeting[:tag].is_a?(Array) ? current_targeting[:tag] : (current_targeting[:tag] ? [current_targeting[:tag]] : [])
  selected_tags = Array(targeting_params[:tags]).reject(&:blank?)

  # Disable tags that are no longer selected
  (current_tags - selected_tags).each do |tag|
    @feature.disable_for_tag(tag) if tag.present?
  end

  # Enable newly selected tags
  (selected_tags - current_tags).each do |tag|
    @feature.enable_for_tag(tag) if tag.present?
  end

  # Handle user IDs - replace existing user targeting
  if targeting_params[:user_ids].present?
    user_ids = targeting_params[:user_ids].split(',').map(&:strip).reject(&:blank?)
    current_user_ids = current_targeting[:user].is_a?(Array) ? current_targeting[:user] : (current_targeting[:user] ? [current_targeting[:user]] : [])

    # Disable users that are no longer in the list
    (current_user_ids - user_ids).each do |user_id|
      @feature.disable_for_user(user_id) if user_id.present?
    end

    # Enable new users
    (user_ids - current_user_ids).each do |user_id|
      @feature.enable_for_user(user_id) if user_id.present?
    end
  elsif targeting_params.key?(:user_ids) && targeting_params[:user_ids].blank?
    # Clear all user targeting if field was cleared
    current_user_ids = current_targeting[:user].is_a?(Array) ? current_targeting[:user] : (current_targeting[:user] ? [current_targeting[:user]] : [])
    current_user_ids.each do |user_id|
      @feature.disable_for_user(user_id) if user_id.present?
    end
  end

  # Handle percentage of users
  percentage_users_value = targeting_params[:percentage_users]
  if percentage_users_value.present? && percentage_users_value.to_s.strip != ''
    percentage = percentage_users_value.to_f
    if percentage > 0 && percentage <= 100
      result = @feature.enable_percentage_of_users(percentage)
      Rails.logger.debug "Magick: Enabled percentage_users #{percentage} for #{@feature.name}: #{result}" if defined?(Rails)
    else
      # Value is 0 or invalid - disable
      @feature.disable_percentage_of_users
    end
  else
    # Field is empty - disable if it was previously set
    @feature.disable_percentage_of_users if current_targeting[:percentage_users]
  end

  # Handle percentage of requests
  percentage_requests_value = targeting_params[:percentage_requests]
  if percentage_requests_value.present? && percentage_requests_value.to_s.strip != ''
    percentage = percentage_requests_value.to_f
    if percentage > 0 && percentage <= 100
      result = @feature.enable_percentage_of_requests(percentage)
      Rails.logger.debug "Magick: Enabled percentage_requests #{percentage} for #{@feature.name}: #{result}" if defined?(Rails)
    else
      # Value is 0 or invalid - disable
      @feature.disable_percentage_of_requests
    end
  else
    # Field is empty - disable if it was previously set
    @feature.disable_percentage_of_requests if current_targeting[:percentage_requests]
  end

  # Handle excluded user IDs
  if targeting_params[:excluded_user_ids].present?
    excluded_user_ids = targeting_params[:excluded_user_ids].split(',').map(&:strip).reject(&:blank?)
    current_excluded_users = current_targeting[:excluded_users].is_a?(Array) ? current_targeting[:excluded_users] : (current_targeting[:excluded_users] ? [current_targeting[:excluded_users]] : [])

    (current_excluded_users - excluded_user_ids).each do |user_id|
      @feature.remove_user_exclusion(user_id) if user_id.present?
    end

    (excluded_user_ids - current_excluded_users).each do |user_id|
      @feature.exclude_user(user_id) if user_id.present?
    end
  elsif targeting_params.key?(:excluded_user_ids) && targeting_params[:excluded_user_ids].blank?
    current_excluded_users = current_targeting[:excluded_users].is_a?(Array) ? current_targeting[:excluded_users] : (current_targeting[:excluded_users] ? [current_targeting[:excluded_users]] : [])
    current_excluded_users.each do |user_id|
      @feature.remove_user_exclusion(user_id) if user_id.present?
    end
  end

  # Handle excluded roles
  current_excluded_roles = current_targeting[:excluded_roles].is_a?(Array) ? current_targeting[:excluded_roles] : (current_targeting[:excluded_roles] ? [current_targeting[:excluded_roles]] : [])
  selected_excluded_roles = Array(targeting_params[:excluded_roles]).reject(&:blank?)

  (current_excluded_roles - selected_excluded_roles).each do |role|
    @feature.remove_role_exclusion(role) if role.present?
  end

  (selected_excluded_roles - current_excluded_roles).each do |role|
    @feature.exclude_role(role) if role.present?
  end

  # Handle excluded tags
  current_excluded_tags = current_targeting[:excluded_tags].is_a?(Array) ? current_targeting[:excluded_tags] : (current_targeting[:excluded_tags] ? [current_targeting[:excluded_tags]] : [])
  selected_excluded_tags = Array(targeting_params[:excluded_tags]).reject(&:blank?)

  (current_excluded_tags - selected_excluded_tags).each do |tag|
    @feature.remove_tag_exclusion(tag) if tag.present?
  end

  (selected_excluded_tags - current_excluded_tags).each do |tag|
    @feature.exclude_tag(tag) if tag.present?
  end

  # After all targeting updates, ensure we're using the registered feature instance
  # and reload it to get the latest state from adapter
  feature_name = @feature.name.to_s
  if Magick.features.key?(feature_name)
    @feature = Magick.features[feature_name]
    @feature.reload
  else
    @feature.reload
  end

  redirect_to magick_admin_ui.feature_path(@feature.name), notice: 'Targeting updated successfully'
rescue StandardError => e
  Rails.logger.error "Magick: Error updating targeting: #{e.message}\n#{e.backtrace.first(5).join("\n")}" if defined?(Rails)
  redirect_to magick_admin_ui.feature_path(@feature.name), alert: "Error updating targeting: #{e.message}"
end