Skip to content

Commit

Permalink
update event settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Powell committed Oct 5, 2015
1 parent a563664 commit 4f853a7
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 30 deletions.
6 changes: 4 additions & 2 deletions app/concepts/power.rb
Expand Up @@ -21,8 +21,10 @@ def initialize(user)
end

power(:creatable_events) { @user.presence && events }
power(:updatable_events) { events }
power(:destroyable_events) { events }
power(:updatable_events) do
@user.admin? && events || events.administered_by(@user)
end
power(:destroyable_events) { updatable_events }

power :assignable_event_fields do
[:name, :slug, :starts_on, :ends_on]
Expand Down
23 changes: 17 additions & 6 deletions app/controllers/events_controller.rb
Expand Up @@ -16,12 +16,16 @@ def new
end

def create
create_event = CreateEvent.new(current_user, event_params)
create_event.call
respond_with_success(create_event)
create_or_update_event(CreateEvent.new(current_user, event_params))
end

rescue ActiveRecord::RecordInvalid
respond_with_failure(create_event)
def edit
current_power.updatable_event!(@event)
end

def update
current_power.updatable_event!(@event)
create_or_update_event(UpdateEvent.new(@event, event_params))
end

private
Expand All @@ -34,9 +38,16 @@ def load_event
@event ||= events.find_by(slug: params[:id])
end

def create_or_update_event(service)
service.call
respond_with_success(service)
rescue ActiveRecord::RecordInvalid
respond_with_failure(service)
end

def respond_with_success(service)
respond_to do |format|
format.html { redirect_to(service.event) }
format.html { redirect_to(edit_event_path(service.event)) }
format.json { render(json: service.event) }
end
end
Expand Down
5 changes: 5 additions & 0 deletions app/helpers/forms_helper.rb
Expand Up @@ -3,6 +3,11 @@ def inline_errors_for(instance, field)
error_messages_for(instance, field).join
end

def save_button(form)
action = form.object.new_record? ? "create" : "save"
button_tag(t(".#{action}"), type: :submit)
end

private

def error_messages_for(instance, field)
Expand Down
4 changes: 4 additions & 0 deletions app/models/event.rb
Expand Up @@ -14,6 +14,10 @@ def to_param
slug
end

def self.administered_by(user)
joins(:administrators).where(administrators: { user_id: user.id })
end

private

def generate_slug
Expand Down
14 changes: 14 additions & 0 deletions app/services/events/update_event.rb
@@ -0,0 +1,14 @@
class UpdateEvent
attr_reader :event

delegate :errors, to: :event

def initialize(event, params)
@event = event
@params = params
end

def call
@event.update!(@params)
end
end
20 changes: 20 additions & 0 deletions app/views/events/_form.html.haml
@@ -0,0 +1,20 @@
= form_for @event do |form|
= @event.errors.inspect

= form.label(:name)
= form.text_field(:name)
= inline_errors_for(@event, :name)

= form.label(:slug)
= form.text_field(:slug)
= inline_errors_for(@event, :slug)

= form.label(:starts_on)
= form.date_field(:starts_on)
= inline_errors_for(@event, :starts_on)

= form.label(:ends_on)
= form.date_field(:ends_on)
= inline_errors_for(@event, :ends_on)

= save_button(form)
1 change: 1 addition & 0 deletions app/views/events/edit.html.haml
@@ -0,0 +1 @@
= render "form"
21 changes: 1 addition & 20 deletions app/views/events/new.html.haml
@@ -1,20 +1 @@
= form_for @event do |form|
= @event.errors.inspect

= form.label(:name)
= form.text_field(:name)
= inline_errors_for(@event, :name)

= form.label(:slug)
= form.text_field(:slug)
= inline_errors_for(@event, :slug)

= form.label(:starts_on)
= form.date_field(:starts_on)
= inline_errors_for(@event, :starts_on)

= form.label(:ends_on)
= form.date_field(:ends_on)
= inline_errors_for(@event, :ends_on)

= button_tag t(".create"), type: :submit
= render "form"
3 changes: 2 additions & 1 deletion config/locales/en.yml
Expand Up @@ -10,5 +10,6 @@ en:
updated: "Your profile was updated"

events:
new:
form:
create: "Create event"
save: "Save settings"
2 changes: 1 addition & 1 deletion features/events/create_event.feature
Expand Up @@ -8,7 +8,7 @@ Feature: Create an event
When I visit the new event page
And I enter my event details
And I click the "Create event" button
Then I should be on the event's page
Then I should be on the event settings page
And I should see the event's name

Scenario: Attempt to create an event when not logged in
Expand Down
14 changes: 14 additions & 0 deletions features/events/edit_event.feature
@@ -0,0 +1,14 @@
Feature: Edit event settings
In order to manage my event
As an event administrator
I want to edit my event's settings

Scenario: Change event name
Given I am logged in as an event administrator
When I visit the event settings page
And I enter a new event name
And I click the "Save settings" button
Then I should be on the event settings page
And I should see "updated"
And I should see the new event name
And the event should have been updated
33 changes: 33 additions & 0 deletions features/step_definitions/event_steps.rb
Expand Up @@ -2,10 +2,25 @@
@event = FactoryGirl.create(:event)
end

Given(/^I am an event administrator$/) do
step("I am an existing user") unless @user
step("an event exists")
Administrator.create(user: @user, event: @event)
end

Given(/^I am logged in as an event administrator$/) do
step("I am logged in")
step("I am an event administrator")
end

When(/^I visit the new event page$/) do
visit(new_event_path)
end

When(/^I visit the event settings page$/) do
visit(edit_event_path(@event))
end

When(/^I enter (?:my|the) event details$/) do
@event ||= FactoryGirl.build(:event).tap(&:validate)
fill_in("Name", with: @event.name)
Expand All @@ -14,6 +29,11 @@
fill_in("End date", with: @event.ends_on)
end

When(/^I enter a new event name$/) do
@new_event_name = "New event name"
fill_in("Name", with: @new_event_name)
end

Then(/^I should be on the event's page$/) do
expect(page.current_path).to eq(event_path(@event))
end
Expand All @@ -22,6 +42,19 @@
expect(page.current_path).to eq(new_event_path)
end

Then(/^I should be on the event settings page$/) do
expect(page.current_path).to eq(edit_event_path(@event))
end

Then(/^I should see the event's name$/) do
expect(page).to have_content(@event.name)
end

Then(/^I should see the new event name$/) do
expect(page).to have_content(@new_event_name)
end

Then(/^the event should have been updated$/) do
@event.reload
expect(@event.name).to eq(@new_event_name)
end
29 changes: 29 additions & 0 deletions spec/services/events/update_event_spec.rb
@@ -0,0 +1,29 @@
require "rails_helper"

describe UpdateEvent do
subject { service.call }
let(:service) { UpdateEvent.new(event, params) }
let(:event) { FactoryGirl.create(:event) }
let(:params) { { name: new_name } }
let(:new_name) { "Updated name" }

it { is_expected.to be true }

it "does not raise an error" do
expect { subject }.not_to raise_error
end

it "updates the name" do
service.call
event.reload
expect(event.name).to eq(new_name)
end

context "without an event name" do
let(:new_name) { "" }

it "raises an error" do
expect { subject }.to raise_error(ActiveRecord::RecordInvalid)
end
end
end

0 comments on commit 4f853a7

Please sign in to comment.