Files
redux-scraper/app/controllers/global_states_controller.rb
Dylan Knutson ec26e425c6 Add FA Cookies management functionality
- Introduced methods for managing FurAffinity cookies in the GlobalStatesController, including `fa_cookies`, `edit_fa_cookies`, and `update_fa_cookies`.
- Added a new policy for managing FA cookies, restricting access to admin users.
- Created views for displaying and editing FA cookies, enhancing user interaction.
- Updated routes to include paths for FA cookies management.
- Added comprehensive tests for the new functionality in the GlobalStatesController spec.
2024-12-30 01:39:21 +00:00

111 lines
2.7 KiB
Ruby

class GlobalStatesController < ApplicationController
before_action :set_global_state, only: %i[edit update destroy]
after_action :verify_authorized
FA_COOKIE_KEYS = %w[
furaffinity-cookie-a
furaffinity-cookie-b
furaffinity-cookie-oaid
].freeze
def index
authorize GlobalState
@global_states = policy_scope(GlobalState).order(:key)
end
def new
@global_state = GlobalState.new
authorize @global_state
end
def create
@global_state = GlobalState.new(global_state_params)
authorize @global_state
if @global_state.save
redirect_to global_states_path,
notice: "Global state was successfully created."
else
render :new, status: :unprocessable_entity
end
end
def edit
authorize @global_state
end
def update
authorize @global_state
if @global_state.update(global_state_params)
redirect_to global_states_path,
notice: "Global state was successfully updated."
else
render :edit, status: :unprocessable_entity
end
end
def destroy
authorize @global_state
@global_state.destroy
redirect_to global_states_path,
notice: "Global state was successfully deleted."
end
def fa_cookies
authorize GlobalState
@fa_cookies =
FA_COOKIE_KEYS.map do |key|
GlobalState.find_by(key: key) ||
GlobalState.new(key: key, value_type: :string)
end
end
def edit_fa_cookies
authorize GlobalState
@fa_cookies =
FA_COOKIE_KEYS.map do |key|
GlobalState.find_by(key: key) ||
GlobalState.new(key: key, value_type: :string)
end
end
def update_fa_cookies
authorize GlobalState
begin
ActiveRecord::Base.transaction do
fa_cookies_params.each do |key, value|
state = GlobalState.find_or_initialize_by(key: key)
state.value = value
state.value_type = :string
state.save!
end
end
redirect_to fa_cookies_global_states_path,
notice: "FA cookies were successfully updated."
rescue ActiveRecord::RecordInvalid => e
@fa_cookies =
FA_COOKIE_KEYS.map do |key|
GlobalState.find_by(key: key) ||
GlobalState.new(key: key, value_type: :string)
end
flash.now[:alert] = "Error updating FA cookies: #{e.message}"
render :edit_fa_cookies, status: :unprocessable_entity
end
end
private
def set_global_state
@global_state = GlobalState.find(params[:id])
end
def global_state_params
params.require(:global_state).permit(:key, :value, :value_type)
end
def fa_cookies_params
params.require(:fa_cookies).permit(*FA_COOKIE_KEYS)
end
end