Skip to content

Commit

Permalink
473 create page text (#476)
Browse files Browse the repository at this point in the history
* add PageTexts model, table

* update factory, add validations and validation tests

* update seed files to create page text for organization

* add dependent destroy to page text association

* add association test for page text

* update organizations create service to create page text when organization is created, and add test
  • Loading branch information
MooseCowBear authored Feb 29, 2024
1 parent dd4b5be commit 25ce9ae
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ class Organization < ApplicationRecord

has_one :profile, dependent: :destroy, class_name: "OrganizationProfile", required: true
has_one :location, through: :profile
has_one :page_text, dependent: :destroy
end
25 changes: 25 additions & 0 deletions app/models/page_text.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# == Schema Information
#
# Table name: page_texts
#
# id :bigint not null, primary key
# about :text
# hero :string
# created_at :datetime not null
# updated_at :datetime not null
# organization_id :bigint not null
#
# Indexes
#
# index_page_texts_on_organization_id (organization_id)
#
# Foreign Keys
#
# fk_rails_... (organization_id => organizations.id)
#
class PageText < ApplicationRecord
acts_as_tenant(:organization)

validates :hero, presence: true, allow_nil: true
validates :about, presence: true, allow_nil: true
end
7 changes: 7 additions & 0 deletions app/services/organizations/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def signal(args)
create_staff_account
add_admin_role_to_staff_account
send_email
create_page_text
end
rescue => e
raise "An error occurred: #{e.message}"
Expand Down Expand Up @@ -100,4 +101,10 @@ def send_email
)
.create_new_org_and_admin(@organization.slug).deliver_now
end

def create_page_text
ActsAsTenant.with_tenant(@organization) do
PageText.create!
end
end
end
11 changes: 11 additions & 0 deletions db/migrate/20240225160909_create_page_texts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreatePageTexts < ActiveRecord::Migration[7.0]
def change
create_table :page_texts do |t|
t.references :organization, null: false, foreign_key: true
t.string :hero
t.text :about

t.timestamps
end
end
end
12 changes: 11 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions db/seeds/01_alta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,6 @@
end

@match.assign_checklist_template(@checklist_template)

PageText.create!(hero: nil, about: nil)
end
2 changes: 2 additions & 0 deletions db/seeds/02_baja.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,6 @@
end

@match.assign_checklist_template(@checklist_template)

PageText.create!(hero: nil, about: nil)
end
6 changes: 6 additions & 0 deletions test/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@
organization { ActsAsTenant.current_tenant }
end

factory :page_text do
hero { "MyString" }
about { Faker::Lorem.sentence }
organization { ActsAsTenant.current_tenant }
end

factory :pet do
birth_date { Faker::Date.backward(days: 7) }
breed { Faker::Creature::Dog.breed }
Expand Down
1 change: 1 addition & 0 deletions test/models/organization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ class OrganizationTest < ActiveSupport::TestCase
should have_many(:pets)

should have_one(:profile).dependent(:destroy).required
should have_one(:page_text).dependent(:destroy)
end
end
8 changes: 8 additions & 0 deletions test/models/page_text_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "test_helper"

class PageTextTest < ActiveSupport::TestCase
context "validations" do
should validate_presence_of(:hero).allow_nil
should validate_presence_of(:about).allow_nil
end
end
25 changes: 25 additions & 0 deletions test/services/organizations/create_service_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "test_helper"

class Organizations::CreateServiceTest < ActiveSupport::TestCase
test "it creates page_text when organization is created" do
args = {
location: {
country: "Mexico",
city_town: "La Ventana",
province_state: "Baja"
},
organization: {
name: "Baja Pet Rescue",
slug: "baja"
},
user: {
email: "[email protected]",
first_name: "Jimmy",
last_name: "Hendrix"
}
}

Organizations::CreateService.new.signal(args)
assert_not Organization.last.page_text.nil?
end
end

0 comments on commit 25ce9ae

Please sign in to comment.