Add ProfileFieldGroupsController to API (#10124)
This commit is contained in:
parent
11c7b148da
commit
adbdbb9789
7 changed files with 92 additions and 1 deletions
12
app/controllers/profile_field_groups_controller.rb
Normal file
12
app/controllers/profile_field_groups_controller.rb
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
class ProfileFieldGroupsController < ApplicationController
|
||||
def index
|
||||
relation = ProfileFieldGroup.includes(:profile_fields)
|
||||
@profile_field_groups = onboarding? ? relation.onboarding : relation.all
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def onboarding?
|
||||
params[:onboarding] == "true"
|
||||
end
|
||||
end
|
||||
|
|
@ -2,4 +2,8 @@ class ProfileFieldGroup < ApplicationRecord
|
|||
has_many :profile_fields, dependent: :nullify
|
||||
|
||||
validates :name, presence: true, uniqueness: true
|
||||
|
||||
scope :onboarding, lambda {
|
||||
includes(:profile_fields).where(profile_fields: { show_in_onboarding: true })
|
||||
}
|
||||
end
|
||||
|
|
|
|||
21
app/views/profile_field_groups/index.json.jbuilder
Normal file
21
app/views/profile_field_groups/index.json.jbuilder
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
json.profile_field_groups do
|
||||
json.array!(@profile_field_groups) do |profile_field_group|
|
||||
json.extract!(
|
||||
profile_field_group,
|
||||
:id,
|
||||
:name,
|
||||
:description,
|
||||
)
|
||||
json.profile_fields(profile_field_group.profile_fields) do |profile_field|
|
||||
json.extract!(
|
||||
profile_field,
|
||||
:id,
|
||||
:attribute_name,
|
||||
:description,
|
||||
:input_type,
|
||||
:label,
|
||||
:placeholder_text,
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -276,6 +276,7 @@ Rails.application.routes.draw do
|
|||
end
|
||||
|
||||
resource :onboarding, only: :show
|
||||
resources :profile_field_groups, only: %i[index], defaults: { format: :json }
|
||||
|
||||
get "/verify_email_ownership", to: "email_authorizations#verify", as: :verify_email_authorizations
|
||||
get "/search/tags" => "search#tags"
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ FactoryBot.define do
|
|||
input_type { :text_field }
|
||||
description { "some description" }
|
||||
placeholder_text { "john.doe@example.com" }
|
||||
show_in_onboarding { false }
|
||||
|
||||
trait :onboarding do
|
||||
show_in_onboarding { true }
|
||||
|
|
|
|||
|
|
@ -1,9 +1,22 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe ProfileFieldGroup, type: :model do
|
||||
subject { create(:profile_field_group) }
|
||||
let!(:group) { create(:profile_field_group) }
|
||||
subject { group }
|
||||
|
||||
it { is_expected.to have_many(:profile_fields).dependent(:nullify) }
|
||||
it { is_expected.to validate_presence_of(:name) }
|
||||
it { is_expected.to validate_uniqueness_of(:name) }
|
||||
|
||||
describe ".onboarding" do
|
||||
let!(:other_group) { create(:profile_field_group) }
|
||||
let!(:profile_field1) { create(:profile_field, :onboarding, profile_field_group: group) }
|
||||
let!(:profile_field2) { create(:profile_field, profile_field_group: other_group) }
|
||||
|
||||
it "only returns groups that have fields for onboarding" do
|
||||
groups = described_class.onboarding
|
||||
expect(groups).to include(group)
|
||||
expect(groups).not_to include(other_group)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
39
spec/requests/profile_field_groups_request_spec.rb
Normal file
39
spec/requests/profile_field_groups_request_spec.rb
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe "ProfileFieldGroups", type: :request do
|
||||
describe "GET /api/profile_field_groups" do
|
||||
|
||||
before { sign_in(create(:user)) }
|
||||
|
||||
let!(:group1) { create(:profile_field_group) }
|
||||
let!(:group2) { create(:profile_field_group) }
|
||||
let!(:field1) { create(:profile_field, :onboarding, label: "Field 1", profile_field_group: group1) }
|
||||
let!(:field2) { create(:profile_field, label: "Field 2", profile_field_group: group1) }
|
||||
let!(:field3) { create(:profile_field, label: "Field 3", profile_field_group: group2) }
|
||||
|
||||
it "returns a successful response" do
|
||||
get profile_field_groups_path
|
||||
expect(response.status).to eq 200
|
||||
end
|
||||
|
||||
it "returns all groups with all fields by default" do
|
||||
get profile_field_groups_path
|
||||
json_response = JSON.parse(response.body, symbolize_names: true)
|
||||
expect(json_response[:profile_field_groups].size).to eq 2
|
||||
end
|
||||
|
||||
it "returns only groups with onboarding fields when onboarding=true" do
|
||||
get profile_field_groups_path, params: { onboarding: true }
|
||||
json_response = JSON.parse(response.body, symbolize_names: true)
|
||||
expect(json_response[:profile_field_groups].size).to eq 1
|
||||
end
|
||||
|
||||
it "only returns the onboarding fields in the group", :aggregate_failures do
|
||||
get profile_field_groups_path, params: { onboarding: true }
|
||||
json_response = JSON.parse(response.body, symbolize_names: true)
|
||||
group = json_response[:profile_field_groups].first
|
||||
expect(group[:profile_fields].size).to eq 1
|
||||
expect(group[:profile_fields].first[:id]).to eq field1.id
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Reference in a new issue