Skip to content

Commit

Permalink
Create StorePatron table
Browse files Browse the repository at this point in the history
  • Loading branch information
binarygit committed Jul 25, 2024
1 parent 79c01d8 commit 6cf24de
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
6 changes: 6 additions & 0 deletions spec/dummy/app/avo/resources/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@ def fields
# Example for error message when resource is missing
field :location, as: :has_one
end

field :patrons, as: :has_many, through: :patronships,
translation_key: "patrons",
extra: -> {
field :review, as: :text
}
end
end
3 changes: 3 additions & 0 deletions spec/dummy/app/models/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@
#
class Store < ApplicationRecord
has_one :location

has_many :patronships, class_name: :StorePatron
has_many :patrons, through: :patronships, class_name: :User, source: :user
end
6 changes: 6 additions & 0 deletions spec/dummy/app/models/store_patron.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class StorePatron < ApplicationRecord
belongs_to :store
belongs_to :user

validates :review, presence: true
end
11 changes: 11 additions & 0 deletions spec/dummy/db/migrate/20240724090242_create_store_patrons.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateStorePatrons < ActiveRecord::Migration[6.1]
def change
create_table :store_patrons do |t|
t.integer :store_id
t.integer :user_id
t.string :review

t.timestamps
end
end
end
10 changes: 9 additions & 1 deletion spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "price_cents", default: 0, null: false
t.string "price_currency", default: "'USD'::character varying", null: false
t.string "price_currency", default: "USD", null: false
end

create_table "projects", force: :cascade do |t|
Expand Down Expand Up @@ -208,6 +208,14 @@
t.index ["user_id"], name: "index_reviews_on_user_id"
end

create_table "store_patrons", force: :cascade do |t|
t.integer "store_id"
t.integer "user_id"
t.string "review"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "stores", force: :cascade do |t|
t.string "name"
t.string "size"
Expand Down
27 changes: 27 additions & 0 deletions spec/features/avo/has_many_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,33 @@
expect(page).not_to have_text post.name
end
end

describe "attaching a team member" do
let(:store) { create(:store) }
let(:url) { "/admin/resources/stores/#{store.id}/patrons/new?view=show" }

it "allows to pass in data for extra fields" do
visit url
expect(page).to have_selector "input#fields_review"
expect(page).to have_selector "select#fields_related_id"

select user.name

expect {
click_button "Attach"
}.not_to change(StorePatron, :count)

expect(page).to have_text "Attachment unsuccessful"

select user.name
fill_in id: "fields_review", with: "Toilet paper is phenomenal here."

expect {
click_button "Attach"
}.to change(StorePatron, :count).by 1
expect(page).to have_text "User attached"
end
end
end

describe "scope" do
Expand Down

0 comments on commit 6cf24de

Please sign in to comment.