Skip to content

Commit

Permalink
Merge pull request sharkipedia#973 from redrambles/import-publish-int…
Browse files Browse the repository at this point in the history
…erface

Allow contributor to toggle published state of their own observation
  • Loading branch information
redrambles authored Jul 26, 2023
2 parents 70b2313 + 03663cc commit e934f89
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 3 deletions.
12 changes: 12 additions & 0 deletions app/assets/stylesheets/observations.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,15 @@
margin-bottom: 10px;
}
}

.column.title-publish {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 2rem;
margin-bottom: 1rem;

h3 {
margin: 0;
}
}
7 changes: 7 additions & 0 deletions app/controllers/observations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ def destroy
end
end

def toggle_publish_state
@observation = Observation.find params[:id]
authorize @observation
@observation.toggle_publish_state
redirect_to @observation.import
end

private

# Use callbacks to share common setup or constraints between actions.
Expand Down
8 changes: 8 additions & 0 deletions app/models/observation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@ def title

[species_names, references_names].reject(&:blank?).join(" - ")
end

def published?
!hidden?
end

def toggle_publish_state
update(hidden: !hidden?)
end
end
4 changes: 4 additions & 0 deletions app/policies/observation_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def destroy?
user.admin?
end

def toggle_publish_state?
user.admin? || record.import&.user == user
end

class Scope < Scope
def resolve
if user&.admin?
Expand Down
4 changes: 3 additions & 1 deletion app/views/imports/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@

<% @import.observations.each do |observation| %>
<div class="columns">
<div class="column">
<div class="column title-publish">
<h3 class="subtitle"><%= observation.title %></h3>
<%= button_to observation.published? ? 'Unpublish' : 'Publish', toggle_publish_state_observation_path(observation), class: "button #{observation.published? ? "is-warning" : "is-success"}", id: "publish-toggle-button"%>
</div>

<div class="column is-one-third">
Expand All @@ -174,3 +175,4 @@
<% end %>
<% end %>
<% end %>

2 changes: 1 addition & 1 deletion app/views/observations/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<div class="control">
<%= f.check_box :hidden, checked: false %><br />
</div>
<p class="help"> Choice to embargo data. If checked then data will be hidden from main daptabase. Can be released by an administrator.</p>
<p class="help"> Choice to embargo data. If checked then data will be hidden from main database. Can be released by an administrator.</p>
</div>
</div>
</div>
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@

resources :trends
resources :traits, only: [:index, :show]
resources :observations
resources :observations do
post "toggle_publish_state", on: :member
end

resources :species, only: [:index, :show]
resources :protected_species, only: [:index]
Expand Down
22 changes: 22 additions & 0 deletions spec/models/observation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@
it { is_expected.not_to include(hidden_observation) }
end

describe "#published?" do
subject { observation.published? }
let!(:observation) { create(:observation) }

it { is_expected.to be_truthy }

context "when hidden" do
let!(:observation) { create(:observation, :unpublished) }

it { is_expected.to be_falsey }
end
end

describe "#toggle_publish_state" do
subject { observation.toggle_publish_state }
let!(:observation) { create(:observation) }

it "toggles the hidden attribute" do
expect { subject }.to change { observation.reload.published? }.from(true).to(false)
end
end

describe "#species through measurements" do
let(:species) { create(:species) }
let(:observation) { create(:observation) }
Expand Down
6 changes: 6 additions & 0 deletions spec/system/new_trait_observation_form_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def fill_measurement(value)
select2 reference.name, css: "#reference_selector", search: true
fill_in "Depth", with: "100"

check "observation_hidden"
# Measurement #1
click_link "Add Measurement"
within(:xpath, "//fieldset[@class='measurement'][1]") do
Expand All @@ -71,6 +72,11 @@ def fill_measurement(value)

expect(import).to be_pending_review

expect(page).to have_content("Publish")

click_button "publish-toggle-button"
expect(page).to have_content("Unpublish")

visit traits_path

expect(page).not_to have_content(import.title)
Expand Down

0 comments on commit e934f89

Please sign in to comment.