diff --git a/app/controllers/profile_field_groups_controller.rb b/app/controllers/profile_field_groups_controller.rb new file mode 100644 index 000000000..6ac739739 --- /dev/null +++ b/app/controllers/profile_field_groups_controller.rb @@ -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 diff --git a/app/models/profile_field_group.rb b/app/models/profile_field_group.rb index 251f14641..c0e02afc5 100644 --- a/app/models/profile_field_group.rb +++ b/app/models/profile_field_group.rb @@ -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 diff --git a/app/views/profile_field_groups/index.json.jbuilder b/app/views/profile_field_groups/index.json.jbuilder new file mode 100644 index 000000000..0bf985bb8 --- /dev/null +++ b/app/views/profile_field_groups/index.json.jbuilder @@ -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 diff --git a/config/routes.rb b/config/routes.rb index dbab354dc..b59a8bab8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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" diff --git a/spec/factories/profile_fields.rb b/spec/factories/profile_fields.rb index edbd3fad3..71a4a4b70 100644 --- a/spec/factories/profile_fields.rb +++ b/spec/factories/profile_fields.rb @@ -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 } diff --git a/spec/models/profile_field_group_spec.rb b/spec/models/profile_field_group_spec.rb index f3953382a..bdcf1d652 100644 --- a/spec/models/profile_field_group_spec.rb +++ b/spec/models/profile_field_group_spec.rb @@ -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 diff --git a/spec/requests/profile_field_groups_request_spec.rb b/spec/requests/profile_field_groups_request_spec.rb new file mode 100644 index 000000000..35770f108 --- /dev/null +++ b/spec/requests/profile_field_groups_request_spec.rb @@ -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