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

Issues controller update fix #741

Merged
merged 4 commits into from
Aug 16, 2016
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
17 changes: 8 additions & 9 deletions app/controllers/api/v1/issues_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,17 @@ def show
end

def update
# TODO: allow for deletion of individual EntityWithIssues
issue_params_copy = issue_params
entities_with_issues_params = issue_params_copy.delete(:entities_with_issues).try(:compact)
@issue.update!(issue_params_copy)
entities_with_issues_params.each do |ewi|
ewi_params[:entity] = OnestopId.find!(ewi_params.delete(:onestop_id))
@issue.entities_with_issues << EntityWithIssues.find_or_initialize_by(ewi_params)
# allowing addition and deletion of EntityWithIssues
# An Issue's entities_with_issues will be completely replaced by update request, if specified
unless entities_with_issues_params.nil?
@issue.entities_with_issues.each { |ewi| @issue.entities_with_issues.delete(ewi) }
entities_with_issues_params.each do |ewi|
ewi[:entity] = OnestopId.find!(ewi.delete(:onestop_id))
@issue.entities_with_issues << EntityWithIssues.create(ewi)
end
end
render json: @issue
end
Expand Down Expand Up @@ -83,11 +87,6 @@ def create

private

def set_entity_with_issues_params(ewi_params)
ewi_params[:entity] = OnestopId.find!(ewi_params.delete(:onestop_id))
@issue.entities_with_issues << EntityWithIssues.find_or_initialize_by(ewi_params)
end

def set_issue
@issue = Issue.find(params[:id])
end
Expand Down
23 changes: 23 additions & 0 deletions spec/controllers/api/v1/issue_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,29 @@
post :update, id: 1, issue: issue
expect(Issue.find(1).details).to eq "This is a test of updating"
end

it 'avoids deleting EntitiesWithIssues when param not supplied' do
issue = {
"details": "This is a test of updating"
}
post :update, id: 1, issue: issue
expect(Issue.find(1).entities_with_issues.size).to eq 3
end

it 'creates specified EntitiesWithIssues and deletes existing EntitiesWithIssues' do
issue = {
"details": "This is a test of updating",
entities_with_issues: [
{
"onestop_id": 's-9qscwx8n60-nyecountyairportdemo',
"entity_attribute": 'geometry'
}
]
}
post :update, id: 1, issue: issue
expect(Issue.find(1).entities_with_issues.size).to eq 1
expect(Issue.find(1).entities_with_issues[0].entity.onestop_id).to eq 's-9qscwx8n60-nyecountyairportdemo'
end
end

context 'POST destroy' do
Expand Down