Skip to content

Commit

Permalink
Extract partials for publishing buttons on unpublished_events#index a…
Browse files Browse the repository at this point in the history
…nd event#show

Also moves the generation of the 'Publish' confirm text from
a "data: confirm" to a bespoke AJAX request. This allows the
controller to not care about fetching the # of users in each
region just to show a confirm dialog.

To facilitate this, added a /regions/:id.json which can
provide the name and # of users to be emailed for a specific region.
  • Loading branch information
tjgrathwell committed May 23, 2016
1 parent b6936d3 commit 94f4797
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 87 deletions.
7 changes: 0 additions & 7 deletions app/controllers/events/unpublished_events_controller.rb
Expand Up @@ -4,13 +4,6 @@ class Events::UnpublishedEventsController < ApplicationController

def index
authorize Event, :see_unpublished?
regions = Region.includes(:users)
.where('users.allow_event_email = ?', true)
.references(:users)
@region_user_counts = regions.each_with_object({}) do |region, hsh|
hsh[region.id] = region.users.length
end

@events = EventPolicy::Scope.new(current_user, Event)
.publishable
.upcoming
Expand Down
6 changes: 0 additions & 6 deletions app/controllers/events_controller.rb
Expand Up @@ -50,12 +50,6 @@ def show
Role::VOLUNTEER => @event.ordered_rsvps(Role::VOLUNTEER, waitlisted: true).to_a,
Role::STUDENT => @event.ordered_rsvps(Role::STUDENT, waitlisted: true).to_a
}
regions = Region.includes(:users)
.where('users.allow_event_email = ?', true)
.references(:users)
@region_user_counts = regions.each_with_object({}) do |region, hsh|
hsh[region.id] = region.users.length
end
end

def new
Expand Down
29 changes: 18 additions & 11 deletions app/controllers/regions_controller.rb
Expand Up @@ -9,18 +9,25 @@ def index

def show
skip_authorization
@region_events = (
@region.events.published_or_visible_to(current_user).includes(:location) +
@region.external_events
).sort_by(&:ends_at)
respond_to do |format|
format.html do
@region_events = (
@region.events.published_or_visible_to(current_user).includes(:location) +
@region.external_events
).sort_by(&:ends_at)

if @region.has_leader?(current_user)
@organizer_rsvps = Rsvp.
group(:user_id, :user_type).
joins([event: [location: :region]]).
includes(:user).
select("user_id, user_type, count(*) as events_count").
where('regions.id' => @region.id, role_id: Role::ORGANIZER.id, user_type: 'User')
if @region.has_leader?(current_user)
@organizer_rsvps = Rsvp.
group(:user_id, :user_type).
joins([event: [location: :region]]).
includes(:user).
select("user_id, user_type, count(*) as events_count").
where('regions.id' => @region.id, role_id: Role::ORGANIZER.id, user_type: 'User')
end
end
format.json do
render json: @region
end
end
end

Expand Down
7 changes: 7 additions & 0 deletions app/models/region.rb
Expand Up @@ -22,4 +22,11 @@ def has_leader?(user)
def destroyable?
(locations_count + external_events_count) == 0
end

def as_json(options = {})
{
name: name,
users_subscribed_to_email_count: users.where(allow_event_email: true).count
}
end
end
18 changes: 3 additions & 15 deletions app/views/events/show.html.erb
Expand Up @@ -13,23 +13,11 @@
<% end %>
<% if @can_publish && !@event.published? %>
<% if @event.location %>
<% if @event.email_on_approval %>
<%= link_to 'Publish', unpublished_event_publish_path(@event, send_mail: true), class: 'btn fa-before', method: :post,
data: {confirm: "Are you sure? This will email #{pluralize(@region_user_counts[@event.location.region.id], 'member')} of #{@event.location.region.name}"} %>
<% else %>
<%= link_to 'Publish', unpublished_event_publish_path(@event), class: 'btn fa-before', method: :post,
data: {confirm: "Are you sure? The event will start showing for all users, and no one will be emailed since the event organizers have chosen to manually send the announcement email."} %>
<% end %>
<% else %>
<button class="btn" disabled>No Location - Can't Publish!</button>
<%= render 'events/unpublished_events/publish_event_button', event: @event %>
<% if current_user.admin? || current_user.publisher? %>
<%= render 'events/unpublished_events/mark_as_spam_button', event: @event %>
<% end %>
<% if current_user.admin? || current_user.publisher? %>
<%= link_to 'Flag as Spam', unpublished_event_flag_path(@event), class: 'btn btn-danger', method: :post,
data: {confirm: "Are you sure? This will remove this event from the approval page, and flag #{@event.organizers.first.full_name} as a spammer so that all subsequent events they create will immediately be flagged as spam."} %>
<% end %>
<% end %>
</div>
</div>
<% end %>
Expand Down
@@ -0,0 +1,4 @@
<%= button_to 'Flag as Spam', unpublished_event_flag_path(event), class: 'btn btn-danger', method: :post,
data: {
confirm: "Are you sure? This will remove this event from the approval page, and flag #{event.organizers.first.full_name} as a spammer so that all subsequent events they create will immediately be flagged as spam."
} %>
24 changes: 24 additions & 0 deletions app/views/events/unpublished_events/_publish_event_button.html.erb
@@ -0,0 +1,24 @@
<% if event.location %>
<%= button_to 'Publish', unpublished_event_publish_path(event), class: 'btn', id: "publish-event-#{event.id}", method: :post %>
<script>
window.whenReady(function () {
$("#publish-event-<%= event.id %>").on('click', function (e) {
e.preventDefault();
$.get('/regions/<%= event.region.id %>.json', function (region) {
var confirmText;
<% if event.email_on_approval %>
confirmText = "Are you sure? This will email " + region.users_subscribed_to_email_count + " members of " + region.name;
<% else %>
confirmText = "Are you sure? The event will start showing for all users, and no one will be emailed since the event organizers have chosen to manually send the announcement email.";
<% end %>
var answer = confirm(confirmText);
if (answer) {
$(e.target).closest('form').submit();
}
});
});
});
</script>
<% else %>
<button class="btn" disabled>No Location - Can't Publish!</button>
<% end %>
15 changes: 2 additions & 13 deletions app/views/events/unpublished_events/_unpublished_event.html.erb
Expand Up @@ -14,20 +14,9 @@
<% end %>
</div>
<div class='event-actions'>
<% if event.location %>
<% if event.email_on_approval %>
<%= button_to 'Publish', unpublished_event_publish_path(event, send_mail: true), class: 'btn', method: :post,
data: {confirm: "Are you sure? This will email #{pluralize(@region_user_counts[event.location.region.id], 'member')} of #{event.location.region.name}"} %>
<% else %>
<%= button_to 'Publish', unpublished_event_publish_path(event), class: 'btn', method: :post,
data: {confirm: "Are you sure? The event will start showing for all users, and no one will be emailed since the event organizers have chosen to manually send the announcement email."} %>
<% end %>
<% else %>
<button class="btn" disabled>No Location - Can't Publish!</button>
<% end %>
<%= render 'publish_event_button', event: event %>
<% if current_user.admin? || current_user.publisher? %>
<%= button_to 'Flag as Spam', unpublished_event_flag_path(event), class: 'btn btn-danger', method: :post,
data: {confirm: "Are you sure? This will remove this event from the approval page, and flag #{event.organizers.first.full_name} as a spammer so that all subsequent events they create will immediately be flagged as spam."} %>
<%= render 'mark_as_spam_button', event: event %>
<% end %>
</div>
</div>
34 changes: 0 additions & 34 deletions spec/controllers/events/unpublished_events_controller_spec.rb
Expand Up @@ -41,40 +41,6 @@
end
end
end

describe 'region user counts' do
before do
sign_in create(:user, publisher: true)

@region1 = @event.region
@region1.update_attributes(name: 'RailsBridge Shellmound')
@region2 = create(:region, name: 'RailsBridge Meriloft')

user_none = create(:user)

user_region1 = create(:user)
user_region1.regions << @region1

user_region2 = create(:user)
user_region2.regions << @region2

user_both_regions = create(:user)
user_both_regions.regions << @region1
user_both_regions.regions << @region2

user_no_email = create(:user, allow_event_email: false)
user_no_email.regions << @region1
end

it "assigns a hash of region/user counts" do
get :index

expect(assigns(:region_user_counts)).to eq({
@region1.id => 2,
@region2.id => 2
})
end
end
end

describe "POST #publish" do
Expand Down
8 changes: 8 additions & 0 deletions spec/controllers/regions_controller_spec.rb
Expand Up @@ -40,6 +40,14 @@
before do
@user = create(:user)
sign_in @user
@user.regions << @region
end

it "can retrieve a JSON representation of a region" do
get :show, id: @region.id, format: :json
json = JSON.parse(response.body)
expect(json['name']).to eq(@region.name)
expect(json['users_subscribed_to_email_count']).to eq(1)
end

context "when rendering views" do
Expand Down
4 changes: 3 additions & 1 deletion spec/features/announcing_an_event_spec.rb
Expand Up @@ -67,7 +67,9 @@

sign_in_as admin
visit unpublished_events_path
click_on "Publish"
accept_confirm do
click_on "Publish"
end

sign_in_as(user_organizer)
end
Expand Down

0 comments on commit 94f4797

Please sign in to comment.