Skip to content

Commit

Permalink
feat(plans): add views for users
Browse files Browse the repository at this point in the history
  • Loading branch information
castrolem committed Aug 15, 2019
1 parent bb60a8b commit 0b9362a
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/assets/stylesheets/plans.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
Place all the styles related to the matching controller here.
They will automatically be included in application.css.
*/
22 changes: 22 additions & 0 deletions app/controllers/plans_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

class PlansController < ApplicationController
before_action :set_plan, only: [:show]

# GET /plans
# GET /plans.json
def index
@plans = Plan.all
end

# GET /plans/1
# GET /plans/1.json
def show; end

private

# Use callbacks to share common setup or constraints between actions.
def set_plan
@plan = Plan.find(params[:id])
end
end
2 changes: 2 additions & 0 deletions app/helpers/plans_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module PlansHelper
end
2 changes: 2 additions & 0 deletions app/views/plans/_plan.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.extract! plan, :id, :created_at, :updated_at
json.url plan_url(plan, format: :json)
23 changes: 23 additions & 0 deletions app/views/plans/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<p id="notice"><%= notice %></p>

<h1>Plans</h1>

<table>
<thead>
<tr>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @plans.each do |plan| %>
<tr>
<td><%= plan.name %></td>
<td><%= plan.description %></td>
<td>$<%= plan.amount %></td>
<td><%= link_to 'Show', plan %></td>
</tr>
<% end %>
</tbody>
</table>

1 change: 1 addition & 0 deletions app/views/plans/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @plans, partial: "plans/plan", as: :plan
7 changes: 7 additions & 0 deletions app/views/plans/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<p id="notice"><%= notice %></p>

<h1><%= @plan.name %></h1>
<p><%= @plan.description %></p>
<p>$<%= @plan.amount %></p>

<%= link_to 'Back', plans_path %>
1 change: 1 addition & 0 deletions app/views/plans/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! "plans/plan", plan: @plan
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

Rails.application.routes.draw do
resources :plans, only: %i[show index]
resources :users, only: %i[show new edit update create destroy]

namespace :admin do
Expand Down
27 changes: 27 additions & 0 deletions spec/controllers/plans_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe PlansController, type: :controller do
let(:valid_attributes) do
FactoryBot.attributes_for(:plan)
end

let(:valid_session) { {} }

describe 'GET #index' do
it 'returns a success response' do
Plan.create! valid_attributes
get :index, params: {}, session: valid_session
expect(response).to be_successful
end
end

describe 'GET #show' do
it 'returns a success response' do
plan = Plan.create! valid_attributes
get :show, params: { id: plan.to_param }, session: valid_session
expect(response).to be_successful
end
end
end
15 changes: 15 additions & 0 deletions spec/routing/plans_routing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe PlansController, type: :routing do
describe 'routing' do
it 'routes to #index' do
expect(get: '/plans').to route_to('plans#index')
end

it 'routes to #show' do
expect(get: '/plans/1').to route_to('plans#show', id: '1')
end
end
end
27 changes: 27 additions & 0 deletions spec/views/plans/index.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'plans/index', type: :view do
let(:plan_1) { FactoryBot.attributes_for(:plan) }
let(:plan_2) { FactoryBot.attributes_for(:plan) }

before(:each) do
assign(:plans, [
Plan.create!(plan_1),
Plan.create!(plan_2)
])
end

it 'renders a list of plans' do
render
assert_select 'tr>td', text: plan_1[:name].to_s
assert_select 'tr>td', text: plan_2[:name].to_s

assert_select 'tr>td', text: plan_1[:description].to_s
assert_select 'tr>td', text: plan_2[:description].to_s

assert_select 'tr>td', text: "$#{plan_1[:amount]}"
assert_select 'tr>td', text: "$#{plan_2[:amount]}"
end
end
18 changes: 18 additions & 0 deletions spec/views/plans/show.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'plans/show', type: :view do
let(:plan) { FactoryBot.attributes_for(:plan) }

before(:each) do
@plan = assign(:plan, Plan.create!(plan))
end

it 'renders attributes' do
render
expect(rendered).to match(/#{plan[:name]}/)
expect(rendered).to match(/#{plan[:description]}/)
expect(rendered).to match(/\$#{plan[:amount]}/)
end
end

0 comments on commit 0b9362a

Please sign in to comment.