Skip to content

Commit

Permalink
Merge pull request #42 from marclerodrigues/feature/add-users-relatio…
Browse files Browse the repository at this point in the history
…n-to-entries

Feature/add users relation to entries
  • Loading branch information
marclerodrigues authored Dec 8, 2019
2 parents 3ca5da1 + c4a46f6 commit 2fafa35
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 59 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ group :development, :test do
gem 'selenium-webdriver'
gem 'pry'
gem 'pry-nav'
gem 'shoulda-matchers'
end

group :test do
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ GEM
selenium-webdriver (3.142.6)
childprocess (>= 0.5, < 4.0)
rubyzip (>= 1.2.2)
shoulda-matchers (4.1.2)
activesupport (>= 4.2.0)
simplecov (0.17.1)
docile (~> 1.1)
json (>= 1.8, < 3)
Expand Down Expand Up @@ -263,6 +265,7 @@ DEPENDENCIES
rspec-rails
sass-rails (~> 5)
selenium-webdriver
shoulda-matchers
simplecov
spring
spring-watcher-listen (~> 2.0.0)
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/entries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def create
format.json { render :show, status: :created, location: @entry }
else
flash.now[:error] = 'Sorry, an error has occured'
format.html { render :new }
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @entry.errors, status: :unprocessable_entity }
end
end
Expand All @@ -49,7 +49,7 @@ def update
format.json { render :show, status: :ok, location: @entry }
else
flash.now[:error] = 'Sorry, an error has occured'
format.html { render :edit }
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @entry.errors, status: :unprocessable_entity }
end
end
Expand All @@ -74,6 +74,6 @@ def set_entry

# Never trust parameters from the scary internet, only allow the white list through.
def entry_params
params.require(:entry).permit(:feeling, :description, :day, :hour)
params.require(:entry).permit(:feeling, :description, :day, :hour, :user_id)
end
end
1 change: 1 addition & 0 deletions app/models/entry.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class Entry < ApplicationRecord
belongs_to :user
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ class User < ApplicationRecord
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable

has_many :entries, dependent: :destroy
end
5 changes: 5 additions & 0 deletions db/migrate/20191208030319_add_users_to_entries.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUsersToEntries < ActiveRecord::Migration[6.0]
def change
add_reference :entries, :user, foreign_key: true, index: true
end
end
5 changes: 4 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_10_20_164632) do
ActiveRecord::Schema.define(version: 2019_12_08_030319) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -22,6 +22,8 @@
t.datetime "updated_at", precision: 6, null: false
t.date "day"
t.time "hour"
t.bigint "user_id"
t.index ["user_id"], name: "index_entries_on_user_id"
end

create_table "users", force: :cascade do |t|
Expand All @@ -37,4 +39,5 @@
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

add_foreign_key "entries", "users"
end
62 changes: 28 additions & 34 deletions spec/controllers/entries_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
# This should return the minimal set of attributes required to create a valid
# Entry. As you add validations to Entry, be sure to
# adjust the attributes here as well.
let(:user) { create(:user) }
let(:valid_attributes) {
{ feeling: 'awesome', description: 'WoooWW', hour: 22 }
attributes_for(:entry, user_id: user.id)
}

let(:invalid_attributes) {
{ other_feeling: 'bad mood', age: 66, admin: true }
attributes_for(:entry, user_id: nil)
}

# This should return the minimal set of values that should be in the session
Expand All @@ -42,16 +43,20 @@
let(:valid_session) { {} }

describe "GET #index" do
before do
create(:entry, valid_attributes)
end

it "returns a success response" do
Entry.create! valid_attributes
get :index, params: {}, session: valid_session
expect(response).to be_successful
end
end

describe "GET #show" do
let(:entry) { create(:entry, valid_attributes) }

it "returns a success response" do
entry = Entry.create! valid_attributes
get :show, params: {id: entry.to_param}, session: valid_session
expect(response).to be_successful
end
Expand All @@ -65,9 +70,10 @@
end

describe "GET #edit" do
let(:entry) { create(:entry, valid_attributes) }

it "returns a success response" do
entry = Entry.create! valid_attributes
get :edit, params: {id: entry.to_param}, session: valid_session
get :edit, params: { id: entry.to_param }, session: valid_session
expect(response).to be_successful
end
end
Expand All @@ -76,45 +82,41 @@
context "with valid params" do
it "creates a new Entry" do
expect {
post :create, params: {entry: valid_attributes}, session: valid_session
post :create, params: { entry: valid_attributes }, session: valid_session
}.to change(Entry, :count).by(1)
end

it "redirects to the created entry" do
post :create, params: {entry: valid_attributes}, session: valid_session
post :create, params: { entry: valid_attributes }, session: valid_session
expect(response).to redirect_to(Entry.last)
end

it "render success flash message" do
post :create, params: {entry: valid_attributes}, session: valid_session
post :create, params: { entry: valid_attributes }, session: valid_session
expect(flash[:success]).to_not be_nil
end

it "render success flash message text" do
post :create, params: {entry: valid_attributes}, session: valid_session
post :create, params: { entry: valid_attributes }, session: valid_session
expect(flash[:success]).to match(/Entry was successfully created./)
end
end

context "with invalid params" do
it "returns a failure response (i.e. to display the 'new' template)" do
post :create, params: {entry: invalid_attributes}, session: valid_session
post :create, params: { entry: invalid_attributes }, session: valid_session
expect(response).not_to be_successful
end

it "render failure flash message" do
skip('Missing template')

expect_any_instance_of(Entry).to receive(:save).and_return(false)
post :create, params: {entry: invalid_attributes}, session: valid_session
post :create, params: { entry: invalid_attributes }, session: valid_session
expect(flash[:error]).to_not be_nil
end

it "render failure flash message text" do
skip('Missing template')

expect_any_instance_of(Entry).to receive(:save).and_return(false)
post :create, params: {entry: invalid_attributes}, session: valid_session
post :create, params: { entry: invalid_attributes }, session: valid_session
expect(flash[:error]).to match(/Sorry, an error has occured/)
end
end
Expand All @@ -123,54 +125,46 @@
describe "PUT #update" do
context "with valid params" do
let(:new_attributes) {
skip("Add a hash of attributes valid for your model")
attributes_for(:entry, feeling: 'Good', user_id: user.id)
}
let(:entry) { create(:entry, user_id: user.id) }

it "updates the requested entry" do
entry = Entry.create! valid_attributes
put :update, params: {id: entry.to_param, entry: new_attributes}, session: valid_session
put :update, params: { id: entry.to_param, entry: new_attributes }, session: valid_session
entry.reload
skip("Add assertions for updated state")
expect(entry.feeling).to eq('Good')
end

it "redirects to the entry" do
entry = Entry.create! valid_attributes
put :update, params: {id: entry.to_param, entry: valid_attributes}, session: valid_session
put :update, params: { id: entry.to_param, entry: valid_attributes }, session: valid_session
expect(response).to redirect_to(entry)
end

it "render success flash message" do
entry = Entry.create! valid_attributes
put :update, params: {id: entry.to_param, entry: valid_attributes}, session: valid_session
put :update, params: { id: entry.to_param, entry: valid_attributes }, session: valid_session
expect(flash[:success]).to_not be_nil
end

it "render success flash message text" do
entry = Entry.create! valid_attributes
put :update, params: {id: entry.to_param, entry: valid_attributes}, session: valid_session
put :update, params: { id: entry.to_param, entry: valid_attributes }, session: valid_session
expect(flash[:success]).to match(/Entry was successfully updated./)
end
end

context "with invalid params" do
let(:entry) { create(:entry, valid_attributes) }

it "returns a success response (i.e. to display the 'edit' template)" do
entry = Entry.create! valid_attributes
put :update, params: {id: entry.to_param, entry: invalid_attributes}, session: valid_session
expect(response).not_to be_successful
end

it "render failure flash message" do
skip('Missing template')

entry = Entry.create! valid_attributes
put :update, params: {id: entry.to_param, entry: invalid_attributes}, session: valid_session
expect(flash[:error]).to_not be_nil
end

it "render failure flash message text" do
skip('Missing template')

entry = Entry.create! valid_attributes
put :update, params: {id: entry.to_param, entry: invalid_attributes}, session: valid_session
expect(flash[:error]).to match(/Sorry, an error has occured/)
end
Expand Down
3 changes: 2 additions & 1 deletion spec/factories/entries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
description { Faker::Lorem.words(number: 10) }
hour { Time.now }
day { Date.today}
user
end
end
end
6 changes: 4 additions & 2 deletions spec/models/entry_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'rails_helper'
require "rails_helper"

RSpec.describe Entry, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
describe "relations" do
it { is_expected.to belong_to(:user) }
end
end
6 changes: 4 additions & 2 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'rails_helper'
require "rails_helper"

RSpec.describe User, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
describe "relations" do
it { is_expected.to have_many(:entries).dependent(:destroy) }
end
end
23 changes: 10 additions & 13 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
# This file is copied to spec/ when you run "rails generate rspec:install"
require "spec_helper"
ENV["RAILS_ENV"] ||= "test"

require File.expand_path('../config/environment', __dir__)
require File.expand_path("../config/environment", __dir__)

# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require "rspec/rails"
require "simplecov"

if ENV['RAILS_ENV'] == 'test'
require 'simplecov'
SimpleCov.start 'rails'
puts "required simplecov"
end
SimpleCov.start "rails"

# Add additional requires below this line. Rails is not loaded until this point!

Expand All @@ -29,7 +26,7 @@
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }
Dir[Rails.root.join("spec", "support", "**", "*.rb")].each { |f| require f }

# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
Expand All @@ -41,10 +38,10 @@
end
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
# Remove this line if you"re not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# If you"re not using ActiveRecord, or you"d prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
Expand Down
4 changes: 1 addition & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require 'capybara/rspec'
require "capybara/rspec"
require "selenium/webdriver"
require 'simplecov'
SimpleCov.start 'rails'
# This file was generated by the `rails generate rspec:install` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
Expand Down
6 changes: 6 additions & 0 deletions spec/support/shoulda_matchers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end

0 comments on commit 2fafa35

Please sign in to comment.