Skip to content

Instantly share code, notes, and snippets.

@balvig
Created April 24, 2019 06:37
Show Gist options
  • Save balvig/9c2e45afd1d9b9ca6741bcee402e7c25 to your computer and use it in GitHub Desktop.
Save balvig/9c2e45afd1d9b9ca6741bcee402e7c25 to your computer and use it in GitHub Desktop.
class EventsController < ApplicationController
def index
+ @events = [Event.new]
end
end
class EventsController < ApplicationController
def index
end
end
Rails.application.routes.draw do
resources :events, only: :index
end
class Event
def visitor
User.last # hardcoded for now
end
def action
"liked" # hardcoded for now
end
def recipe
Recipe.last # hardcoded for now
end
def featured_visitor_recipe
visitor.recipes.last # undecided how to feature a recipe
end
end
<% 3.times do %>
<div class="media">
<%= image_tag "dummy_user.jpg", class: "media__img" %>
<div class="media__body">
Sebastian
<p class="meta">
liked <%= link_to "Quick Pizza Dough", "#" %>
</p>
<div class="attachment media">
<%= image_tag "food.jpg", class: "media__img" %>
<div class="media__body">
<h3 class="recipe-title">
<%= link_to "Grilled Aubergines", "#" %>
</h3>
</div>
</div>
<p class="subtle">Try out their latest recipe</p>
</div>
</div>
<% end %>
class CreateEvents < ActiveRecord::Migration[5.2]
def change
create_table :events do |t|
t.references :visitor, null: false
t.references :recipe, null: false
t.integer :action, null: false
t.timestamps
end
end
end
@@ Model @@
-class Event
- def visitor
- User.last
- end
-
- def action
- "liked"
- end
-
- def recipe
- Recipe.last
- end
+class Event < ApplicationRecord
+ belongs_to :visitor, class_name: "User"
+ belongs_to :recipe
+ enum action: %i(liked)
def featured_visitor_recipe
visitor.recipes.last
@@ Controller @@
class EventsController < ApplicationController
def index
- @events = [Event.new]
+ @events = Event.all
end
end
scenario "Viewing events" do
visitor = User.create!(name: "Visitor Name")
visitor.recipes.create!(title: "Featured Visitor Recipe")
- Recipe.create!(title: "Visited Recipe")
+ visited_recipe = Recipe.create!(title: "Visited Recipe")
+ Event.create!(visitor: visitor, recipe: visited_recipe, action: :liked)
scenario "Viewing events" do
+ visitor = User.create!(name: "Visitor Name")
+ visitor.recipes.create!(title: "Featured Visitor Recipe")
+ Recipe.create!(title: "Visited Recipe")
+
visit events_path
- expect(page).to have_text "Grilled aubergines"
+ expect(page).to have_text "Visitor Name"
+ expect(page).to have_text "Featured Visitor Recipe"
+ expect(page).to have_text "liked Visited Recipe"
end
scenario "Viewing events" do
visit events_path
expect(page).to have_text "Grilled Aubergines"
end
-<% 3.times do %>
+<% @events.each do |event| %>
<div class="media">
- <%= image_tag "dummy_user.jpg", class: "media__img" %>
+ <%= image_tag event.visitor.avatar, class: "media__img" %>
<div class="media__body">
- Sebastian
+ <%= event.visitor.name %>
<p class="meta">
- liked <%= link_to "Quick Pizza Dough", "#" %>
+ <%= event.action %> <%= link_to event.recipe.title, event.recipe %>
</p>
<div class="attachment media">
- <%= image_tag "food.jpg", class: "media__img" %>
+ <%= image_tag event.featured_visitor_recipe.image, class: "media__img" %>
<div class="media__body">
<h3 class="recipe-title">
- <%= link_to "Grilled Aubergines", "#" %>
+ <%= link_to event.featured_visitor_recipe.title, event.featured_visitor_recipe %>
</h3>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment