Skip to content

Commit

Permalink
Chapter 3: End of chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Bigg committed Apr 25, 2018
1 parent be91ad1 commit 05bb6bf
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 3 deletions.
8 changes: 8 additions & 0 deletions app/controllers/projects/projects_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
module Projects
class ProjectsController < ApplicationController
def index
@projects = repo.all
end

def show
@project = repo.by_id(params[:id])
end

def new
@project = Projects::Project.new
end
Expand Down
6 changes: 3 additions & 3 deletions app/relations/project_relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ProjectRelation < ROM::Relation[:sql]

# define your methods here ie:
#
# def all
# select(:id, :name).order(:id)
# end
def all
select(:id, :name).order(:id)
end
end
8 changes: 8 additions & 0 deletions app/repositories/project_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@ class ProjectRepository < ROM::Repository::Root
commands :create, update: :by_pk, delete: :by_pk

struct_namespace Projects

def all
projects.all
end

def by_id(id)
projects.by_pk(id).one!
end
end
6 changes: 6 additions & 0 deletions app/views/projects/projects/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
<h2>Projects</h2>

<%= link_to "New Project", new_projects_project_path, class: "btn btn-primary" %>

<ul>
<% @projects.each do |project| %>
<li><%= link_to project.name, project %></li>
<% end %>
</ul>
1 change: 1 addition & 0 deletions app/views/projects/projects/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h2><%= @project.name %></h2>
4 changes: 4 additions & 0 deletions projects.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
repo = ProjectRepository.new(ROM.env)
project = repo.all.first
puts project.class.to_s == "Projects::Project"
puts project.respond_to?(:to_model)
15 changes: 15 additions & 0 deletions spec/features/projects/viewing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require "rails_helper"

RSpec.feature "Users can view projects" do
let(:project_repo) { ProjectRepository.new(ROM.env) }
let!(:project) { project_repo.create(name: "Sublime Text 3") }

scenario "with the project details" do
visit "/"
click_link "Sublime Text 3"
expect(page.current_url).to eq projects_project_url(project)
within("h2") do
expect(page).to have_content("Sublime Text 3")
end
end
end

0 comments on commit 05bb6bf

Please sign in to comment.