diff --git a/app/assets/stylesheets/observations.scss b/app/assets/stylesheets/observations.scss index 8de8d91e..0c323a0c 100644 --- a/app/assets/stylesheets/observations.scss +++ b/app/assets/stylesheets/observations.scss @@ -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; + } +} diff --git a/app/controllers/observations_controller.rb b/app/controllers/observations_controller.rb index ac7c3350..b262f748 100644 --- a/app/controllers/observations_controller.rb +++ b/app/controllers/observations_controller.rb @@ -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. diff --git a/app/models/observation.rb b/app/models/observation.rb index 578b10e9..aa09dd2a 100644 --- a/app/models/observation.rb +++ b/app/models/observation.rb @@ -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 diff --git a/app/policies/observation_policy.rb b/app/policies/observation_policy.rb index 9293605e..e77b026e 100644 --- a/app/policies/observation_policy.rb +++ b/app/policies/observation_policy.rb @@ -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? diff --git a/app/views/imports/show.html.erb b/app/views/imports/show.html.erb index bb184d72..1ab9314a 100644 --- a/app/views/imports/show.html.erb +++ b/app/views/imports/show.html.erb @@ -156,8 +156,9 @@ <% @import.observations.each do |observation| %>
-
+

<%= observation.title %>

+ <%= button_to observation.published? ? 'Unpublish' : 'Publish', toggle_publish_state_observation_path(observation), class: "button #{observation.published? ? "is-warning" : "is-success"}", id: "publish-toggle-button"%>
@@ -174,3 +175,4 @@ <% end %> <% end %> <% end %> + diff --git a/app/views/observations/_form.html.erb b/app/views/observations/_form.html.erb index 8f82e086..30e53431 100644 --- a/app/views/observations/_form.html.erb +++ b/app/views/observations/_form.html.erb @@ -21,7 +21,7 @@
<%= f.check_box :hidden, checked: false %>
-

Choice to embargo data. If checked then data will be hidden from main daptabase. Can be released by an administrator.

+

Choice to embargo data. If checked then data will be hidden from main database. Can be released by an administrator.

diff --git a/config/routes.rb b/config/routes.rb index 25a8f135..75d65114 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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] diff --git a/spec/models/observation_spec.rb b/spec/models/observation_spec.rb index 079448d0..8e7b0e68 100644 --- a/spec/models/observation_spec.rb +++ b/spec/models/observation_spec.rb @@ -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) } diff --git a/spec/system/new_trait_observation_form_system_spec.rb b/spec/system/new_trait_observation_form_system_spec.rb index 787b7014..ad56c314 100644 --- a/spec/system/new_trait_observation_form_system_spec.rb +++ b/spec/system/new_trait_observation_form_system_spec.rb @@ -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 @@ -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)