[deploy] Profile Admin UI Setup Screen Refactor (v1.5) (#9649)

* implement some suggestions from https://github.com/forem/forem/pull/9610

* chore: update the errors based on suggestions

* feat: make query readable on blazer
This commit is contained in:
Ridhwana 2020-08-07 23:35:18 +02:00 committed by GitHub
parent d471b0051f
commit a191fd2ca7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 36 additions and 24 deletions

View file

@ -1,5 +1,8 @@
module Admin
class ProfileFieldsController < Admin::ApplicationController
ALLOWED_PARAMS = %i[
input_type label active placeholder_text description
].freeze
layout "admin"
def index
@ -7,26 +10,41 @@ module Admin
end
def update
@profile_fields = ProfileField.find(params[:id])
@profile_fields.update!(profile_field_params)
profile_field = ProfileField.find(params[:id])
if profile_field.update(profile_field_params)
flash[:success] = "Profile field #{profile_field.label} updated"
else
flash[:error] = "Error: #{profile_field.errors_as_sentence}"
end
redirect_to admin_profile_fields_path
end
def create
@profile_field = ProfileField.create(profile_field_params)
profile_field = ProfileField.new(profile_field_params)
if profile_field.save
flash[:success] = "Profile field #{profile_field.label} created"
else
flash[:error] = "Error: #{profile_field.errors_as_sentence}"
end
redirect_to admin_profile_fields_path
end
def destroy
@profile_field = ProfileField.find(params[:id])
@profile_field.destroy
profile_field = ProfileField.find(params[:id])
if profile_field.destroy
flash[:success] = "Profile field #{profile_field.label} deleted"
else
flash[:error] = "Error: #{profile_field.errors_as_sentence}"
end
redirect_to admin_profile_fields_path
end
private
private_constant :ALLOWED_PARAMS
def profile_field_params
allowed_params = %i[input_type label active placeholder_text description]
allowed_params = ALLOWED_PARAMS
params.require(:profile_field).permit(allowed_params)
end
end

View file

@ -1,13 +1,11 @@
class ProfileField < ApplicationRecord
# Key names follow the Rails form helpers
INPUT_TYPES = {
enum input_type: {
text_field: 0,
text_area: 1,
check_box: 2,
color_field: 3
}.freeze
enum input_type: INPUT_TYPES
}
validates :label, presence: true, uniqueness: { case_sensitive: false }
validates :active, inclusion: { in: [true, false] }

View file

@ -12,7 +12,7 @@
</div>
<div class="form-group">
<%= form.label :input_type %>
<%= form.select :input_type, ProfileField::INPUT_TYPES.keys, class: "form-control" %>
<%= form.select :input_type, ProfileField.input_types.keys, class: "form-control" %>
</div>
<div class="form-group">
<%= form.label :active %>

View file

@ -1,9 +1,9 @@
<div class="crayons-card grid p-6 mb-6 gap-1">
<%= form_for [:admin, ProfileField.new], html: { class: "inline-form" } do |form| %>
<div class="form-group">
<%= render "form", form: form %>
<%= form.submit class: "btn btn-primary" %>
</div>
<%= render "form", form: form %>
<%= form.submit class: "btn btn-primary" %>
</div>
<% end %>
</div>

View file

@ -31,6 +31,7 @@ data_sources:
smart_columns:
status: {0: "enqueued", 1: "working", 2: "succeeded", 3: "failed"}
input_types: {0: "text_field", 1: "text_area", 2: "check_box", 3: "color_field"}
# create audits
audit: true

View file

@ -67,7 +67,6 @@ RSpec.configure do |config|
config.include ApplicationHelper
config.include ActionMailer::TestHelper
config.include ActiveJob::TestHelper
config.include Devise::Test::ControllerHelpers, type: :view
config.include Devise::Test::IntegrationHelpers, type: :system
config.include Devise::Test::IntegrationHelpers, type: :request

View file

@ -1,7 +1,6 @@
require "rails_helper"
RSpec.describe "/admin/chat_channels", type: :request do
include ActiveJob::TestHelper
let(:user) { create(:user) }
let(:chat_channel) { create(:chat_channel) }

View file

@ -1,16 +1,13 @@
require "rails_helper"
RSpec.describe "/admin/profile_fields", type: :request do
include ActiveJob::TestHelper
let(:user) { create(:user) }
let(:admin) { create(:user, :super_admin) }
before do
user.add_role(:super_admin)
sign_in user
end
before do
sign_in admin
end
describe "GET /admin/profile_fields" do
let(:profile_field) { create(:profile_field) }
it "renders successfully" do
get admin_profile_fields_path
@ -18,7 +15,7 @@ RSpec.describe "/admin/profile_fields", type: :request do
end
it "lists the profile fields" do
profile_field
profile_field = create(:profile_field)
get admin_profile_fields_path
expect(response.body).to include(
profile_field.label,