Skip to content

Commit

Permalink
838 Rename Form to CustomForm::Form and Question to CustomForm::Quest…
Browse files Browse the repository at this point in the history
…ion (#864)

* Added module custom_form for Form and Question Controller and Views

* Fix test cases for custom_form module for Form and Question

* Nested modules

* Modularize Form model and its related test cases

* Modularize Question Module with CustomForm

* Lint Fix

* Fix seed file

* Modulaize Question policy code and fix link in question form
  • Loading branch information
sarvaiyanidhi authored Jul 8, 2024
1 parent 8974f4c commit 3613dda
Show file tree
Hide file tree
Showing 40 changed files with 1,147 additions and 1,093 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
module Organizations
module Staff
module CustomForm
class FormsController < ApplicationController
before_action :context_authorize!
before_action :set_form, only: %i[show edit update destroy]

layout "dashboard"

def index
@forms = authorized_scope(::CustomForm::Form.all)
end

def new
@form = ::CustomForm::Form.new
end

def create
@form = organization.forms.new(form_params)

if @form.save
redirect_to staff_custom_form_forms_path, notice: t(".success")
else
flash.now[:alert] = t(".error")

render :new, status: :unprocessable_entity
end
end

def show
end

def edit
end

def update
if @form.update(form_params)
redirect_to staff_custom_form_forms_path, notice: t(".success")
else
render :edit, status: :unprocessable_entity
end
end

def destroy
@form.destroy

redirect_to staff_custom_form_forms_path, notice: t(".success")
end

private

def organization
current_user.organization
end

def set_form
@form = organization.forms.find(params[:id])
authorize! @form
rescue ActiveRecord::RecordNotFound
redirect_to staff_custom_form_forms_path, alert: t(".not_found")
end

def form_params
params.require(:custom_form_form).permit(:name, :description, :title, :instructions)
end

def context_authorize!
authorize! ::CustomForm::Form, context: {organization: Current.organization}
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
module Organizations
module Staff
module CustomForm
class QuestionsController < ApplicationController
before_action :context_authorize!
before_action :set_form
before_action :set_question, only: %i[show edit update destroy]

layout "dashboard"

def index
@questions = authorized_scope(@form.questions)
end

def new
@question = @form.questions.new
end

def create
@question = @form.questions.new(question_params)

if @question.save
redirect_to staff_custom_form_form_path(@form), notice: t(".success")
else
flash.now[:alert] = t(".error")

render :new, status: :unprocessable_entity
end
end

def edit
end

def update
if @question.update(question_params)
redirect_to staff_custom_form_form_path(@form), notice: t(".success")
else
flash.now[:alert] = t(".error")

render :edit, status: :unprocessable_entity
end
end

def destroy
@question.destroy

redirect_to staff_custom_form_form_path(@form), notice: t(".success")
end

private

def organization
current_user.organization
end

def set_form
@form = organization.forms.find(params[:form_id])
authorize! @form
rescue ActiveRecord::RecordNotFound
redirect_to staff_custom_form_forms_path, alert: t("organizations.staff.custom_form.forms.not_found")
end

def set_question
@question = @form.questions.find(params[:id])
authorize! @question
rescue ActiveRecord::RecordNotFound
redirect_to staff_custom_form_form_path(@form), alert: t(".not_found")
end

def question_params
params.require(:custom_form_question).permit(:name, :description, :label, :help_text, :required, :input_type, :options)
end

def context_authorize!
authorize! ::CustomForm::Question, context: {organization: Current.organization}
end
end
end
end
end
67 changes: 0 additions & 67 deletions app/controllers/organizations/staff/forms_controller.rb

This file was deleted.

74 changes: 0 additions & 74 deletions app/controllers/organizations/staff/questions_controller.rb

This file was deleted.

12 changes: 7 additions & 5 deletions app/models/form.rb → app/models/custom_form/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
#
# fk_rails_... (organization_id => organizations.id)
#
class Form < ApplicationRecord
acts_as_tenant(:organization)
module CustomForm
class Form < ApplicationRecord
acts_as_tenant(:organization)

has_many :form_profiles, dependent: :destroy
has_many :questions, -> { ordered }, dependent: :destroy
has_many :form_profiles, dependent: :destroy
has_many :questions, -> { ordered }, dependent: :destroy

validates_presence_of :name, :title
validates_presence_of :name, :title
end
end
16 changes: 9 additions & 7 deletions app/models/question.rb → app/models/custom_form/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@
#
# fk_rails_... (form_id => forms.id)
#
class Question < ApplicationRecord
belongs_to :form
module CustomForm
class Question < ApplicationRecord
belongs_to :form, class_name: "CustomForm::Form"

has_one :organization, through: :form
has_one :organization, through: :form

scope :ordered, -> { order(:sort_order) }
scope :ordered, -> { order(:sort_order) }

validates_presence_of :name, :label
validates_presence_of :name, :label

def snapshot
{label:, options:}
def snapshot
{label:, options:}
end
end
end
2 changes: 1 addition & 1 deletion app/models/custom_form/submitted_answer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SubmittedAnswer < ApplicationRecord
belongs_to :question
belongs_to :user

has_one :form, through: :question
has_one :form, class_name: "CustomForm::Form", through: :question
has_one :organization, through: :form
end
end
2 changes: 1 addition & 1 deletion app/models/form_profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# fk_rails_... (form_id => forms.id)
#
class FormProfile < ApplicationRecord
belongs_to :form
belongs_to :form, class_name: "CustomForm::Form"

has_one :organization, through: :form
end
2 changes: 1 addition & 1 deletion app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Organization < ApplicationRecord
has_many :users, through: :staff_accounts
has_many :pets
has_many :default_pet_tasks
has_many :forms, dependent: :destroy
has_many :forms, class_name: "CustomForm::Form", dependent: :destroy
has_many :faqs

has_one :profile, dependent: :destroy, class_name: "OrganizationProfile", required: true
Expand Down
14 changes: 14 additions & 0 deletions app/policies/organizations/custom_form/form_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Organizations
module CustomForm
class FormPolicy < ApplicationPolicy
pre_check :verify_organization!
pre_check :verify_active_staff!

alias_rule :new?, :create?, :index?, to: :manage?

def manage?
permission?(:manage_forms)
end
end
end
end
Loading

0 comments on commit 3613dda

Please sign in to comment.