Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jade dickinson show contributor name #917

Merged
merged 16 commits into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion app/blueprints/blueprint_authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,19 @@ def view(*options, &block)

class Transformer < Blueprinter::Transformer
def transform(hash, object, options)
policy = Pundit.policy(options[:current_user], object)
policy = Pundit.policy(current_user_from(options), object)
policy.restricted_attributes.each do |attr|
hash[attr] = nil
end
end

private

def current_user_from(options)
unless options.key? :current_user
fail ArgumentError, '`current_user` option must be given when rendering a serializer with BlueprintAuthorization'
end
options[:current_user]
end
end
end
2 changes: 1 addition & 1 deletion app/blueprints/configuration_blueprint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ConfigurationBlueprint < Blueprinter::Base
Category.visible.roots.includes(:categories)
end

association :contact_methods, blueprint: ContactMethodBlueprint do
association :contact_methods, blueprint: ContactMethodBlueprint, view: :with_form_fields do
ContactMethod.enabled
end

Expand Down
7 changes: 5 additions & 2 deletions app/blueprints/contact_method_blueprint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ class ContactMethodBlueprint < Blueprinter::Base
identifier :id

field :name
field :field, name: :field_name
field :label, name: :field_label

view :with_form_fields do
field :field, name: :field_name
field :label, name: :field_label
end
end
1 change: 1 addition & 0 deletions app/blueprints/contribution_blueprint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ def routes
Rails.application.routes.url_helpers
end
end
association :person, blueprint: PersonBlueprint
end
2 changes: 1 addition & 1 deletion app/blueprints/person_blueprint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class PersonBlueprint < Blueprinter::Base
identifier :id

fields :name, :email, :phone, :skills
field :preferred_contact_method
association :preferred_contact_method, blueprint: ContactMethodBlueprint

view :with_location do
association :location, blueprint: LocationBlueprint, view: :with_location_type
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/contributions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ def index
@filter_groupings = BrowseFilter.filter_groupings_json
# The BrowserFilter takes the result of the parameters from the filter checkboxes and returns a list of contributions
filter = BrowseFilter.new(allowed_params)
@contributions = ContributionBlueprint.render(filter.contributions, contribution_blueprint_options)
@contributions = ContributionBlueprint.render(
filter.contributions,
show_view_path: true,
current_user: context.user
)
respond_to do |format|
format.html
format.json { render inline: @contributions }
Expand Down Expand Up @@ -55,10 +59,6 @@ def peer_to_peer_mode?
@system_setting.peer_to_peer?
end

def contribution_blueprint_options
{show_view_path: true}
end

def allowed_params
params.permit(:format, BrowseFilter::ALLOWED_PARAMS)
end
Expand Down
9 changes: 7 additions & 2 deletions app/javascript/pages/browse/ListBrowser.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<th>Service Area</th>
<th>Connect</th>
<th>View</th>
<!-- <th>Details</th>-->
<th v-if="showContributorNames">Contributor Name</th>
</tr>
<tr v-for="contribution in contributions" :key="contribution.id">
<td>
Expand All @@ -32,7 +32,7 @@
<a :href="contribution.view_path" class="button icon-list is-primary"><span class=""> View</span></a>
</div>
</td>
<!-- <td>{{ contribution.title }}</td>-->
<td v-if="showContributorNames">{{ contribution.person.name }}</td>
</tr>
</table>
</div>
Expand All @@ -53,5 +53,10 @@ export default {
SingleIcon,
MappedIconList,
},
computed: {
showContributorNames() {
return this.contributions.some(contribution => contribution.person.name)
},
},
}
</script>
11 changes: 5 additions & 6 deletions app/javascript/pages/browse/TileListItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
<div v-if="service_area" class="has-text-grey-lighter">
{{ service_area.name }}
</div>
<div v-if="person.name" class="contributor-name">
{{ `From: ${person.name}` }}
</div>
</div>
</div>
<div class="actions">
<div class="buttonSpacing" v-if="respond_path">
<a :href="respond_path" class="button icon-list is-primary"><span class=""> Respond</span></a>
</div>
<div class="buttonSpacing" v-if="view_path">
<a :href="view_path" class="button icon-list is-primary"><span class=""> View</span></a>
</div>
Expand All @@ -62,10 +62,9 @@ export default {
created_at: Number,
urgency: Object,
contact_types: {type: Array, default: () => []},
profile_path: String,
respond_path: String,
view_path: String,
match_path: String
match_path: String,
person: {type: Object, default: null}
},
components: {
TagList,
Expand Down
16 changes: 16 additions & 0 deletions lib/contributions.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
"location": null,
"match_path": null,
"name": "Offer: accessing healthcare (Dr. Jill Koss)",
"person": {
"id": 1,
"name": "Marcus Garvey"
},
"respond_path": "/contributions/3/respond?locale=en",
"service_area": {
"id": 6,
Expand Down Expand Up @@ -57,6 +61,10 @@
"location": null,
"match_path": null,
"name": "Offer: services (Romeo Leannon)",
"person": {
"id": 1,
"name": "Marcus Garvey"
},
"respond_path": "/contributions/4/respond?locale=en",
"service_area": {
"id": 3,
Expand Down Expand Up @@ -96,6 +104,10 @@
"location": null,
"match_path": null,
"name": "Offer: emotional (Romeo Leannon)",
"person": {
"id": 1,
"name": "Marcus Garvey"
},
"respond_path": "/contributions/5/respond?locale=en",
"service_area": {
"id": 2,
Expand Down Expand Up @@ -135,6 +147,10 @@
"location": null,
"match_path": null,
"name": "Offer: tech support (Romeo Leannon)",
"person": {
"id": 1,
"name": "Marcus Garvey"
},
"respond_path": "/contributions/6/respond?locale=en",
"service_area": {
"id": 4,
Expand Down
10 changes: 10 additions & 0 deletions spec/blueprints/blueprint_authorization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@
expect(serialized_hash).to include(location: nil)
end
end

describe 'current_user option' do
it 'raises an error if current_user is not provided' do
expect { PersonBlueprint.render_as_hash(object) }.to raise_error ArgumentError
end

it 'allows current_user to be nil' do
expect { PersonBlueprint.render_as_hash(object, current_user: nil) }.not_to raise_error
end
end
end

context 'when serializing the parent of a blueprint with authorization' do
Expand Down
16 changes: 14 additions & 2 deletions spec/blueprints/contribution_blueprint_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'rails_helper'

RSpec.describe ContributionBlueprint do
let(:user) { build :user, :admin }
let(:expected_category) { Faker::Lorem.word }
let(:expected_category_id) { create(:category, name: expected_category).id }
let(:contribution) do
Expand Down Expand Up @@ -32,6 +33,17 @@
'match_path' => nil,
'name' => contribution.name,
'location' => nil,
'person' => {
'id' => contribution.person.id,
'name' => contribution.person.name,
'email' => contribution.person.email,
'phone' => contribution.person.phone,
'skills' => contribution.person.skills,
'preferred_contact_method' => {
'id' => contribution.person.preferred_contact_method.id,
'name' => contribution.person.preferred_contact_method.name
}
},
'service_area' => {
'description' => contribution.service_area.description,
'id' => contribution.service_area.id,
Expand All @@ -52,13 +64,13 @@
'description' => contribution.description,
'contact_types' => [{'id' => expected_contact_method.id, 'name' => expected_contact_method.name}]
}]}
result = ContributionBlueprint.render([contribution], root: 'contributions')
result = ContributionBlueprint.render([contribution], root: 'contributions', current_user: user)
expect(JSON.parse(result)).to match(expected_data)
end

it 'emits contribution_path as view_path if the show_view_path option is present' do
expected_path = Rails.application.routes.url_helpers.contribution_path(contribution)
result = ContributionBlueprint.render(contribution, show_view_path: true)
result = ContributionBlueprint.render(contribution, show_view_path: true, current_user: user)
expect(JSON.parse(result)['view_path']).to eq(expected_path)
end
end
16 changes: 8 additions & 8 deletions spec/javascript/pages/browse/ListBrowser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ describe('ListBrowser', () => {

def('tableHeaders', () => $wrapper.findAll('th').wrappers)

describe('respond column', () => {
describe('contributor name column', () => {
it('shows the column with typical data', function () {
assert.isTrue($tableHeaders.some(header => header.text() == 'View'))
assert.isTrue($tableHeaders.some(header => header.text() == 'Contributor Name'))
})

describe('when contributions do not come with response urls', () => {
describe('when contributor names have been witheld based on authorization', () => {
def('contributions', () => {
testData.contributions.map(contribution => {
contribution.view_path = ''
return contribution
testData.contributions.forEach((contribution) => {
contribution.person.name = null // FIXME: this mutates fixture data
})
return testData
})

it('hides the respond button', function() {
assert.isFalse($tableHeaders.some(header => header.text() == 'Respond'))
it('hides the contributor name column', function() {
assert.isFalse($tableHeaders.some(header => header.text() == 'Contributor Name'))
})
})
})
Expand Down
30 changes: 30 additions & 0 deletions spec/javascript/pages/browse/TileListItem.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {createLocalVue, shallowMount} from '@vue/test-utils'
import {configure} from 'vue_config'
import TileListItem from 'pages/browse/TileListItem'

describe('TileListItem', () => {
def('wrapper', () => {
return shallowMount(TileListItem, {
localVue: configure(createLocalVue()),
propsData: $props,
})
})

def('props', () => {
return {
created_at: Date.now(),
person: {name: 'Zapata'}
}
})

describe('contributor name', () => {
it('shows the name when given', () => {
assert.equal($wrapper.get('.contributor-name').text(), 'From: Zapata')
})

it('omits the whole line if name is not given', () => {
$props.person.name = null
assert.isFalse($wrapper.find('.contributor-name').exists())
})
})
})