Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added body to events removed introduction/conclusion and properly display events on the show page #138

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/dashboards/event_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class EventDashboard < Administrate::BaseDashboard
updated_at: Field::DateTime,
title: Field::String,
introduction: Field::Text,
conclusion: Field::Text
conclusion: Field::Text,
body: Field::Text
}.freeze

# COLLECTION_ATTRIBUTES
Expand Down Expand Up @@ -44,6 +45,7 @@ class EventDashboard < Administrate::BaseDashboard
:introduction,
:conclusion,
:state,
:body,
:starts_at,
:location
].freeze
Expand Down
6 changes: 5 additions & 1 deletion app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Event < ActiveRecord::Base
validates :state,
presence: true,
inclusion: { in: STATES }
validates :body, presence: true

enumerize :state, in: STATES, default: :proposed

Expand All @@ -25,7 +26,10 @@ def title_with_date
[title, date].join(" : ")
end

# To change the behaviour in /admin/events/1/edit...
def date
starts_at.strftime("%B %d")
end

# driven by views/fields/enum_field/_show.html.erb
def to_s
title_with_date
Expand Down
12 changes: 11 additions & 1 deletion app/views/events/index.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
<h1>Events#index</h1>
<% @events.each do |event| %>
<div class="post clearfix">
<h2 class="post-title"><%= link_to event.title, event_path(event)%></h2>
<p class="meta">
Starts: <%= event.date %>
</p>
<div class="post-body">
<%= render_markdown_as_html(event.body)%>
</div>
</div>
<% end %>
13 changes: 9 additions & 4 deletions app/views/events/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<h1><%= @event.title %></h1>

<%= render_markdown_as_html(@event.introduction) %>
<%= render_markdown_as_html(@event.conclusion) %>
<div class="post clearfix">
<h2 class="post-title"><%= @event.title%></h2>
<p class="meta">
Starts: <%= @event.date %>
</p>
<div class="post-body">
<%= @event.body %>
</div>
</div>
2 changes: 1 addition & 1 deletion app/views/layouts/_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<h4 class='meetup-title'><%= link_to event.title_with_date, event %></h4>
<div class="meetup-body">
<p>
<%= truncate(event.introduction, length: 400) %>
<%= truncate(event.body, length: 400) %>
</p>
<p>
<%= link_to 'Please RSVP', '#' %>
Expand Down
4 changes: 4 additions & 0 deletions db/migrate/20140929233719_create_events.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class CreateEvents < ActiveRecord::Migration
class Event < ActiveRecord::Base
translates :title, :introduction, :conclusion
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The globalize translations are introduced in this migration then later removed, this causes an error in the rake db:migrate . This is a workaround to the issue @nicholasjhenry reported.


def up
create_table :events do |t|
t.string :type, null: false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class RemoveIntroductionConclusionFromEvent < ActiveRecord::Migration
def change
remove_column :event_translations, :introduction, :text
remove_column :event_translations, :conclusion, :text
end
end
9 changes: 9 additions & 0 deletions db/migrate/20151206045102_add_body_to_event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddBodyToEvent < ActiveRecord::Migration
def up
Event.add_translation_fields! body: :text
end

def down
remove_column :event_translations, :body
end
end
19 changes: 11 additions & 8 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160308004823) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "event_translations", force: :cascade do |t|
t.integer "event_id", null: false
t.string "locale", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "title", null: false
t.text "introduction"
t.text "conclusion"
t.integer "event_id", null: false
t.string "locale", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "title", null: false
t.text "body"
end

add_index "event_translations", ["event_id"], name: "index_event_translations_on_event_id", using: :btree
Expand All @@ -36,6 +35,8 @@
t.integer "location_id", null: false
t.integer "user_id", null: false
t.string "state", default: "proposed"
t.integer "location_id", null: false
t.integer "user_id", null: false
end

add_index "events", ["location_id"], name: "index_events_on_location_id", using: :btree
Expand Down Expand Up @@ -139,6 +140,8 @@
t.string "slug", default: "temporary-slug", null: false
end

add_index "pages", ["slug"], name: "index_pages_on_slug", unique: true, using: :btree

create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
Expand Down
1 change: 1 addition & 0 deletions spec/factories/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
title { Faker::Lorem.sentence }
introduction { Faker::Lorem.sentence }
conclusion { Faker::Lorem.paragraph }
body { Faker::Lorem.paragraph }

trait :proposed do
state "proposed"
Expand Down
6 changes: 6 additions & 0 deletions spec/models/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,11 @@
event = create(:event, author: author)
expect(event.author).to eq author
end

it "does not validate when 'body' is blank" do
event = Event.new(title: nil)
expect(event).to be_invalid
expect(event.errors.messages.keys).to include :body
end
end
end
21 changes: 21 additions & 0 deletions spec/views/events/index.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require "rails_helper"

describe "events/index.html.erb" do
it "displays all the events" do
first_event = create(:event)
second_event = create(:event, title: "event two")
assign(:events, [first_event, second_event])
render

expect(rendered).to match(/#{first_event.title}/)
expect(rendered).to match(/#{second_event.title}/)
end

it "links events title to corresponding show page" do
event = create(:event)
assign(:events, [event])
render

expect(rendered).to have_link "#{event.title}", href: event_path(event)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer to_s over string interpolation.

end
end