Skip to content

Commit 22cd4ce

Browse files
authored
Merge pull request #620 from maebeale/listing-per-category
Generate one Listing per category on form submission
2 parents b27ef92 + 3dd8179 commit 22cd4ce

File tree

13 files changed

+102
-48
lines changed

13 files changed

+102
-48
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Changelog
22

33
## Unreleased
4+
### Enhancements
5+
* Ask/Offer form now generates a listing for each category selected #536, #620
6+
47
### Bugfixes
58
* Ask/Offer form breaks when no custom questions are configured #621, #622
69

app/blueprints/submission_blueprint.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ class SubmissionBlueprint < Blueprinter::Base
99
submission&.person&.location
1010
end
1111

12-
association :listing, blueprint: ListingBlueprint do |submission|
13-
submission.listings.first
14-
end
12+
association :listings, blueprint: ListingBlueprint
1513

1614
field :errors do |submission|
1715
submission.errors.as_json(full_messages: true)

app/controllers/asks_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def create
2323
def submission_params
2424
params[:submission].tap do |p|
2525
p[:form_name] = 'Ask_form'
26-
p[:listing_attributes][:type] = 'Ask'
26+
p[:listings_attributes][:type] = 'Ask'
2727
end
2828
end
2929

app/controllers/offers_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def create
2323
def submission_params
2424
params[:submission].tap do |p|
2525
p[:form_name] = 'Offer_form'
26-
p[:listing_attributes][:type] = 'Offer'
26+
p[:listings_attributes][:type] = 'Offer'
2727
end
2828
end
2929

app/forms/listing_form.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
class ListingForm < BaseForm
22
with_options default: nil do
33
integer :id
4-
record :person
4+
record :category
55
record :location
6+
record :person
67
record :service_area
7-
array :tag_list, default: []
88
string :description
99
string :state
1010
string :type
1111
end
1212

1313
def execute
1414
Listing.find_or_new(id).tap do |listing|
15-
listing.attributes = given_inputs
15+
listing.attributes = given_inputs.
16+
reject{ |k, _v| k == :category }.
17+
merge(tag_list: category.lineage)
1618
end
1719
end
1820
end

app/forms/submission_form.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class SubmissionForm < BaseForm
44
with_options default: nil do
55
integer :id
66
record :service_area
7-
hash :listing_attributes, strip: false
7+
hash :listings_attributes, strip: false
88
hash :location_attributes, strip: false
99
hash :person_attributes, strip: false
1010
string :form_name
@@ -14,7 +14,7 @@ class SubmissionForm < BaseForm
1414
def execute
1515
build_location
1616
build_person
17-
build_listing
17+
build_listings
1818
build_submission_responses
1919
build_submission
2020
end
@@ -29,12 +29,15 @@ def build_person
2929
@person ||= PersonForm.build person_attributes.merge location: @location
3030
end
3131

32-
def build_listing
33-
@listing ||= ListingForm.build listing_attributes.merge(
34-
person: @person,
35-
location: @location,
36-
service_area: service_area
37-
)
32+
def build_listings
33+
@listings = (listings_attributes[:categories] || []).map do |category_id|
34+
ListingForm.build listings_attributes.merge(
35+
category: category_id,
36+
location: @location,
37+
person: @person,
38+
service_area: service_area,
39+
)
40+
end
3841
end
3942

4043
def build_submission
@@ -64,7 +67,7 @@ def submission_attributes
6467
.merge(
6568
body: body_json,
6669
person: @person,
67-
listings: [@listing],
70+
listings: @listings,
6871
submission_responses: @submission_responses,
6972
)
7073
end

app/javascript/components/forms/CategoryFields.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929
<p v-if="description" class="mb-1"> {{ description }} </p>
3030

3131
<!-- If any subcategories are selected, add parent category to list of tags submitted -->
32-
<input v-if="anySelected(subcategories)" :name="fieldNamePrefix" :value="name" type="hidden" />
32+
<input v-if="anySelected(subcategories)" :name="fieldNamePrefix" :value="id" type="hidden" />
3333

3434
<div v-for="{id: subId, name: subName, description: subDescription} in subcategories" :key="subId">
3535
<b-field class="pb-05" grouped>
3636
<b-checkbox
3737
v-model="selectedTags"
3838
:name="fieldNamePrefix"
39-
:native-value="subName"
39+
:native-value="subId"
4040
size="is-medium"
4141
>
4242
<span>{{ subName | capitalize }}</span>

app/javascript/pages/Ask.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
/><SpacerField />
3535

3636
<CategoryFields
37-
:fieldNamePrefix="withListingPrefix('tag_list[]')"
37+
:fieldNamePrefix="withListingPrefix('categories[]')"
3838
:categories="configuration.categories"
3939
:tags="listing.tag_list"
4040
>
@@ -98,7 +98,7 @@ export default {
9898
}
9999
},
100100
created: function() {
101-
this.withListingPrefix = partial(fieldNameWithPrefix, 'submission[listing_attributes]')
101+
this.withListingPrefix = partial(fieldNameWithPrefix, 'submission[listings_attributes]')
102102
this.withPersonPrefix = partial(fieldNameWithPrefix, 'submission[person_attributes]')
103103
},
104104
}

app/javascript/pages/Offer.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
/><SpacerField />
3535

3636
<CategoryFields
37-
:fieldNamePrefix="withListingPrefix('tag_list[]')"
37+
:fieldNamePrefix="withListingPrefix('categories[]')"
3838
:categories="configuration.categories"
3939
:tags="listing.tag_list"
4040
>
@@ -120,12 +120,12 @@ export default {
120120
data() {
121121
return {
122122
location: this.submission.location || {},
123-
listing: this.submission.listing || {},
123+
listing: this.submission.listing || {}, // todo: change to render from multiple listings
124124
person: this.submission.person || {},
125125
}
126126
},
127127
created: function() {
128-
this.withListingPrefix = partial(fieldNameWithPrefix, 'submission[listing_attributes]')
128+
this.withListingPrefix = partial(fieldNameWithPrefix, 'submission[listings_attributes]')
129129
this.withPersonPrefix = partial(fieldNameWithPrefix, 'submission[person_attributes]')
130130
},
131131
skillsMessage,

app/models/category.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@ class Category < ApplicationRecord
2121
def full_name
2222
"#{ parent&.name&.upcase + ": " if parent}#{parent.present? ? name : name.upcase }"
2323
end
24+
25+
def lineage
26+
own = [name]
27+
parent ? parent.lineage + own : own
28+
end
2429
end

0 commit comments

Comments
 (0)