Skip to content

Commit

Permalink
Add hidden class helper to banner form and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
carrollsa committed Aug 11, 2023
1 parent b0baf2b commit 19216e4
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/controllers/banners_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ def new
authorize :application, :admin_or_supervisor?

@banner = Banner.new
@has_alternate_active_banner = current_organization.banners.where(active: true).where.not(id: @banner.id).exists?
@org_has_alternate_active_banner = current_organization.banners.where(active: true).where.not(id: @banner.id).exists?
end

def edit
authorize :application, :admin_or_supervisor?

@banner = current_organization.banners.find(params[:id])
@has_alternate_active_banner = current_organization.banners.where(active: true).where.not(id: @banner.id).exists?
@org_has_alternate_active_banner = current_organization.banners.where(active: true).where.not(id: @banner.id).exists?
end

def create
Expand Down
7 changes: 7 additions & 0 deletions app/helpers/banner_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module BannerHelper
def conditionally_add_hidden_class(current_banner_is_active)
unless current_banner_is_active && @org_has_alternate_active_banner
"d-none"
end
end
end
4 changes: 2 additions & 2 deletions app/views/banners/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
<%= form.text_field :name, required: true %>
</div>
<div data-controller="reveal" data-reveal-hidden-class="d-none" class="form-check checkbox-style mb-20">
<%= form.check_box :active, data: { action: @has_alternate_active_banner && 'click->reveal#toggle' }, class: 'form-check-input' %>
<%= form.check_box :active, data: { action: @org_has_alternate_active_banner && 'click->reveal#toggle' }, class: 'form-check-input' %>
<%= form.label :active, "Active?", class: 'form-check-label' %>
<span data-reveal-target="item" class="text-danger<%= form.object.active && @has_alternate_active_banner ? '' : ' d-none' %>">Warning: This will replace your current active banner</span>
<span data-reveal-target="item" class="text-danger <%= conditionally_add_hidden_class(form.object.active) %>">Warning: This will replace your current active banner</span>
</div>
<div class="input-style-1">
<%= form.label :content %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/banners/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%= render 'banners/form', banner: @banner, has_alternate_active_banner: @has_alternate_active_banner %>
<%= render 'banners/form', banner: @banner, org_has_alternate_active_banner: @org_has_alternate_active_banner %>
2 changes: 1 addition & 1 deletion app/views/banners/new.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%= render 'banners/form', banner: @banner, has_alternate_active_banner: @has_alternate_active_banner %>
<%= render 'banners/form', banner: @banner, org_has_alternate_active_banner: @org_has_alternate_active_banner %>

0 comments on commit 19216e4

Please sign in to comment.