Class: Magick::AdminUI::FeaturesController
- Inherits:
-
ActionController::Base
- Object
- ActionController::Base
- Magick::AdminUI::FeaturesController
- Includes:
- ActionController::RequestForgeryProtection
- Defined in:
- app/controllers/magick/adminui/features_controller.rb
Instance Method Summary collapse
- #available_roles ⇒ Object
- #available_tags ⇒ Object
- #disable ⇒ Object
- #disable_for_role ⇒ Object
- #edit ⇒ Object
- #enable ⇒ Object
- #enable_for_role ⇒ Object
- #enable_for_user ⇒ Object
- #index ⇒ Object
- #magick_admin_ui ⇒ Object
- #partially_enabled?(feature) ⇒ Boolean
- #show ⇒ Object
- #update ⇒ Object
- #update_targeting ⇒ Object
- #update_variants ⇒ Object
Instance Method Details
#available_roles ⇒ Object
33 34 35 |
# File 'app/controllers/magick/adminui/features_controller.rb', line 33 def available_roles Magick::AdminUI.config.available_roles || [] end |
#available_tags ⇒ Object
37 38 39 |
# File 'app/controllers/magick/adminui/features_controller.rb', line 37 def Magick::AdminUI.config. end |
#disable ⇒ Object
101 102 103 104 |
# File 'app/controllers/magick/adminui/features_controller.rb', line 101 def disable @feature.disable redirect_to magick_admin_ui.features_path, notice: 'Feature disabled' end |
#disable_for_role ⇒ Object
121 122 123 124 125 126 127 128 129 |
# File 'app/controllers/magick/adminui/features_controller.rb', line 121 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 |
#edit ⇒ Object
71 72 |
# File 'app/controllers/magick/adminui/features_controller.rb', line 71 def edit end |
#enable ⇒ Object
96 97 98 99 |
# File 'app/controllers/magick/adminui/features_controller.rb', line 96 def enable @feature.enable redirect_to magick_admin_ui.features_path, notice: 'Feature enabled' end |
#enable_for_role ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'app/controllers/magick/adminui/features_controller.rb', line 111 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_user ⇒ Object
106 107 108 109 |
# File 'app/controllers/magick/adminui/features_controller.rb', line 106 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 |
#index ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/controllers/magick/adminui/features_controller.rb', line 46 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_ui ⇒ Object
29 30 31 |
# File 'app/controllers/magick/adminui/features_controller.rb', line 29 def magick_admin_ui Magick::AdminUI::Engine.routes.url_helpers end |
#partially_enabled?(feature) ⇒ Boolean
41 42 43 44 |
# File 'app/controllers/magick/adminui/features_controller.rb', line 41 def partially_enabled?(feature) targeting = feature.instance_variable_get(:@targeting) || {} targeting.any? && !targeting.empty? end |
#show ⇒ Object
68 69 |
# File 'app/controllers/magick/adminui/features_controller.rb', line 68 def show end |
#update ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/controllers/magick/adminui/features_controller.rb', line 74 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_targeting ⇒ Object
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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'app/controllers/magick/adminui/features_controller.rb', line 131 def update_targeting # Handle targeting updates from form targeting_params = params[:targeting] || {} unless hash_like?(targeting_params) redirect_to magick_admin_ui.feature_path(@feature.name), alert: 'Invalid targeting payload.' return end # 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_targeting[:tag].is_a?(Array) ? current_targeting[:tag] : (current_targeting[:tag] ? [current_targeting[:tag]] : []) = Array(targeting_params[:tags]).reject(&:blank?) # Disable tags that are no longer selected ( - ).each do |tag| @feature.disable_for_tag(tag) if tag.present? end # Enable newly selected 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_targeting[:excluded_tags].is_a?(Array) ? current_targeting[:excluded_tags] : (current_targeting[:excluded_tags] ? [current_targeting[:excluded_tags]] : []) = Array(targeting_params[:excluded_tags]).reject(&:blank?) ( - ).each do |tag| @feature.remove_tag_exclusion(tag) if tag.present? end ( - ).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 for #{@feature.name}: #{e.class}: #{e.}\n#{e.backtrace.first(5).join("\n")}" if defined?(Rails) redirect_to magick_admin_ui.feature_path(@feature.name), alert: 'Could not update targeting — see server logs for details.' end |
#update_variants ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'app/controllers/magick/adminui/features_controller.rb', line 288 def update_variants variants_data = [] if params[:variants].present? && !hash_like?(params[:variants]) redirect_to magick_admin_ui.feature_path(@feature.name), alert: 'Invalid variants payload.' return end if params[:variants].present? params[:variants].each do |_index, variant_params| next if variant_params[:name].blank? variants_data << { name: variant_params[:name].strip, value: variant_params[:value].to_s.strip, weight: (variant_params[:weight].presence || 0).to_f } end end if variants_data.any? @feature.set_variants(variants_data) else # Clear variants if all removed @feature.instance_variable_get(:@targeting)&.delete(:variants) @feature.save_targeting end redirect_to magick_admin_ui.feature_path(@feature.name), notice: 'Variants updated successfully' rescue StandardError => e Rails.logger.error "Magick: Error updating variants for #{@feature.name}: #{e.class}: #{e.}" if defined?(Rails) redirect_to magick_admin_ui.feature_path(@feature.name), alert: 'Could not update variants — see server logs for details.' end |