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

Subject editing #9

Merged
merged 48 commits into from
Apr 22, 2018
Merged

Subject editing #9

merged 48 commits into from
Apr 22, 2018

Conversation

mantasavas
Copy link
Owner

@mantasavas mantasavas commented Apr 9, 2018

#5

  • Finished user authentication, the user now can log in (authenticate) in the following ways:
    ** Using his Gmail account (gem => omniauth)
    ** Using regular registration form with email and password (gem => devise)

  • Only authenticated users can access subjects: view, edit, create, delete.

  • Subject content can be written in rich format (gem => trix)

  • Only subject author (creator), can delete, update post, for other people it is only accessible for a view

  • Added rspec user tests

…sary, fixed spacing etc, solved merge issues
@mantasavas mantasavas added the enhancement New feature or request label Apr 9, 2018
Copy link

@povilasjurcys povilasjurcys left a comment

Choose a reason for hiding this comment

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

keeping in mind that it's your first PR I would say that it looks quite good. Few things you need to do:

  1. setup you editor in order to make spacing consistent in all files
  2. Get rid of unnecessary/unrelated files from PR

@@ -0,0 +1,110 @@
# This configuration was generated by

Choose a reason for hiding this comment

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

there is no need to use .rubocop_todo.yml in new project. Main purpose for .rubocop_todo.yml is to make it easier to add rubocop in huge projects with many violations. Since you are starting almost from scratch, I would recommend to get rid of this file and do rubocop autofix instead (rubocop -a )

Copy link
Owner Author

Choose a reason for hiding this comment

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

removed .rubocop_todo.yml from project source, form now on I won't use it in my fresh new projects

Gemfile Outdated
gem 'devise'
gem 'omniauth-google-oauth2'
gem 'omniauth-twitter'
gem 'pry'

Choose a reason for hiding this comment

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

pry, capybara', factory_girl_rails, rspec-railsshould go to:develoment, :test` group. There is no point of having them in production environment

Copy link
Owner Author

Choose a reason for hiding this comment

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

fixed, next time will try not to be absent-minded

Gemfile Outdated
gem 'rails_12factor'
end

group :test do

Choose a reason for hiding this comment

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

if you do not use this group than it's safe to remove it

Copy link
Owner Author

Choose a reason for hiding this comment

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

fixed

@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.

Choose a reason for hiding this comment

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

as I understand this file was generated by rails g scaffold, but there is no point of having it in repo since it's empty

Copy link
Owner Author

Choose a reason for hiding this comment

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

added to .gitignore, removed from repo

@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.

Choose a reason for hiding this comment

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

as I understand this file was generated by rails g scaffold, but there is no point of having it in repo since it's empty

Copy link
Owner Author

Choose a reason for hiding this comment

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

added to .gitignore, removed from repo

describe User do
describe "validating user information" do
it "has a valid factory" do
FactoryGirl.create(:user).should be_valid

Choose a reason for hiding this comment

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

you can create new records with shorter form create(:user)

to do so, you need to include this in your rails_helper.rb:
https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#rspec

Copy link
Owner Author

Choose a reason for hiding this comment

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

Good advise, fixed that as well, and changed FactoryGirl deprecated version to FactoryBot

end

# Returns all users, there emails have a match
def self.find_mathing_letter(letter)

Choose a reason for hiding this comment

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

it this method used anywhere except tests?

Copy link
Owner Author

Choose a reason for hiding this comment

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

In the future, when the user is going to look for other users and their emails (issue #6 ), this function should come in handy, right now I'm not using it, just practicing to write some tests.

describe User do
describe "validating user information" do
it "has a valid factory" do
FactoryGirl.create(:user).should be_valid

Choose a reason for hiding this comment

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

also since you are testing build in validators, I do not see a point of testing them

Copy link
Owner Author

Choose a reason for hiding this comment

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

mostly I used it for learning purposes, my model is almost empty, got you 👍



describe User do
describe "validating user information" do

Choose a reason for hiding this comment

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

in most cases describe part should describe which method are you testing and context part should describe conditions in which tests are run. For example:

describe User do
  describe '#valid?' do # instance methods starts with '#' and class methods starts with '.' (dot)
    context 'when email is missing' do
      #...
    end
  end
  
  describe '.find_mathing_letter' do
    context 'when there is a match'
    context 'when there is no match'
  end
end

Copy link
Owner Author

Choose a reason for hiding this comment

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

thank you, good advise, would try keep that in mind next time

Copy link
Owner Author

Choose a reason for hiding this comment

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

fixed

"locale"=>"en"}}}.to_json

parsedJson = JSON.parse(jsonResponseString, object_class: OpenStruct)
expect(User.from_omniauth(parsedJson)).not_to be_falsey

Choose a reason for hiding this comment

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

you could use double here and reduce test setup dramaticly, For example:

auth_object = double(
      provider: 'google', 
      info: double(email: user.email),
      # ... more methods which are used inside from_omniauth
    )

    expect(User.from_omniauth(auth_object)).not_to be_falsey

Copy link
Owner Author

Choose a reason for hiding this comment

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

fixed, removed instance variable and replaced with double

@ViliusLuneckas
Copy link

That’s a decent review @povilasjurcys 👍

Copy link

@povilasjurcys povilasjurcys left a comment

Choose a reason for hiding this comment

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

Overal looks good, just needs some formatting fixes 👍

@@ -2,7 +2,7 @@

class Subject < ApplicationRecord
validates :title, presence: true, length: { minimum: 5 }
validates :body, presence: true
validates :body, presence: true, length: {minimum: 20}

Choose a reason for hiding this comment

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

can you integrate https://codeclimate.com/ in your project. It will give you comments about inconsistent style instantly

Copy link
Owner Author

Choose a reason for hiding this comment

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

Done, integrated

end
end

let(:user) { FactoryBot.create(:user) }

Choose a reason for hiding this comment

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

keep all let and before statements in one place so it will be easier to track all context changes

Copy link
Owner Author

Choose a reason for hiding this comment

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

Fixed, if I correctly understood you, also fixed the FactoryBot to a shorter version (which you pointed out before)

Gemfile Outdated
@@ -72,6 +72,8 @@ group :development, :test do
gem 'rubocop', require: false
gem 'rubocop-rspec', require: false
gem 'shoulda-matchers', git: 'https://github.com/thoughtbot/shoulda-matchers.git', branch: 'rails-5'
gem 'rails-controller-testing'

Choose a reason for hiding this comment

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

i think this gem should not belong to :development group

end

context 'when user fails to save subject' do
it 'title is too short' do

Choose a reason for hiding this comment

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

tests is your documentation so it would be nice if you could read them like a normal english sentences. For example this particular test will be converted to sentence:

SubjectsController GET #new
  when user fails to save subject 
    title is too short

which is not very human readable. On the other hand you have quite fluent tests in 47 or 52 lines:

SubjectsController GET #new 
  when user saves subject 
    creates the subject and redirect to subject view
    adds subject in subjects table`

try running your specs with documentation formatter:

rspec -f documentation

and you will see more examples

Copy link
Owner Author

Choose a reason for hiding this comment

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

Oh, okay, I will keep that in mind next time, thanks for good advice!

Copy link
Owner Author

Choose a reason for hiding this comment

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

fixed

@@ -0,0 +1,8 @@
# frozen_string_literal: true

class Subject < ApplicationRecord

Choose a reason for hiding this comment

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

Style/Documentation: Missing top-level class documentation comment.

Copy link
Owner Author

Choose a reason for hiding this comment

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

unnecessary

Choose a reason for hiding this comment

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

we try to make rubocop happy. If you disagree with rubocop then you have two ways of solving this problem:
a) add comment # rubocop:disable Style/Documentation
b) disable Style/Documentation rule in .rubocop.yml file for project or directory

validates :email, uniqueness: true
has_many :subjects

def self.from_omniauth(auth)

Choose a reason for hiding this comment

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

Metrics/AbcSize: Assignment Branch Condition size for from_omniauth is too high. [20.62/15]
Metrics/MethodLength: Method has too many lines. [11/10]

:recoverable, :rememberable, :trackable, :validatable, :omniauthable, omniauth_providers: [:google_oauth2]

validates :email, uniqueness: true
has_many :subjects

Choose a reason for hiding this comment

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

Rails/HasManyOrHasOneDependent: Specify a :dependent option.

# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")

Shoulda::Matchers.configure do |config|

Choose a reason for hiding this comment

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

Lint/ShadowingOuterLocalVariable: Shadowing outer local variable - config.


context 'when did not found unmatching latter emails' do
it 'return only t matches' do
described_class.find_matching_letter('t').each { |user| expect(user.email).not_to include(petras.email, povilas.email) }

Choose a reason for hiding this comment

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

Metrics/LineLength: Line is too long. [128/120]

#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20_180_405_132_238) do

Choose a reason for hiding this comment

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

Metrics/BlockLength: Block has too many lines. [26/25]

# frozen_string_literal: true

class DeviseCreateUsers < ActiveRecord::Migration[5.1]
def change

Choose a reason for hiding this comment

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

Metrics/MethodLength: Method has too many lines. [15/10]

# Add a new OmniAuth provider. Check the wiki for more information on setting
# up on your models and hooks.
# config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo'
config.omniauth :google_oauth2, '398135549725-pm9shjiu4fta240nirdbb8baihakf4ph.apps.googleusercontent.com', 'yOBwbclBDVDdB_hsr7PCezIH'

Choose a reason for hiding this comment

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

Metrics/LineLength: Line is too long. [136/120]

config.stretches = Rails.env.test? ? 1 : 11

# Set up a pepper to generate the hashed password.
# config.pepper = '9a0cd2508e19f24dcf762310977ed5b530c7e967d9f92c4011ddba30047ad115c5946236098878e9df43fded3ef3f027fb1e71b9954847666b0c24a722c77f60'

Choose a reason for hiding this comment

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

Metrics/LineLength: Line is too long. [150/120]

# confirmation, reset password and unlock tokens in the database.
# Devise will use the `secret_key_base` as its `secret_key`
# by default. You can change it below and use your own secret key.
# config.secret_key = 'f7ffe60b93af3e9eb4820b35e765b23edd5e61d53cf8c465737e0b1886c01b21748653f27cad511b9c0dd636a70336574f67a765fa22a35a9ef10dffc3b59248'

Choose a reason for hiding this comment

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

Metrics/LineLength: Line is too long. [154/120]

@@ -0,0 +1,8 @@
# frozen_string_literal: true

class Subject < ApplicationRecord

Choose a reason for hiding this comment

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

Style/Documentation: Missing top-level class documentation comment.

validates :email, uniqueness: true
has_many :subjects

def self.from_omniauth(auth)

Choose a reason for hiding this comment

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

Metrics/AbcSize: Assignment Branch Condition size for from_omniauth is too high. [20.62/15]
Metrics/MethodLength: Method has too many lines. [11/10]

Copy link
Owner Author

Choose a reason for hiding this comment

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

fixed

:recoverable, :rememberable, :trackable, :validatable, :omniauthable, omniauth_providers: [:google_oauth2]

validates :email, uniqueness: true
has_many :subjects

Choose a reason for hiding this comment

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

Rails/HasManyOrHasOneDependent: Specify a :dependent option.

Copy link
Owner Author

Choose a reason for hiding this comment

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

fixed

# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")

Shoulda::Matchers.configure do |config|

Choose a reason for hiding this comment

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

Lint/ShadowingOuterLocalVariable: Shadowing outer local variable - config.


context 'when did not found unmatching latter emails' do
it 'return only t matches' do
described_class.find_matching_letter('t').each { |user| expect(user.email).not_to include(petras.email, povilas.email) }

Choose a reason for hiding this comment

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

Metrics/LineLength: Line is too long. [128/120]

#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20_180_405_132_238) do

Choose a reason for hiding this comment

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

Metrics/BlockLength: Block has too many lines. [26/25]

# frozen_string_literal: true

class DeviseCreateUsers < ActiveRecord::Migration[5.1]
def change

Choose a reason for hiding this comment

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

Metrics/MethodLength: Method has too many lines. [15/10]

# Add a new OmniAuth provider. Check the wiki for more information on setting
# up on your models and hooks.
# config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo'
config.omniauth :google_oauth2, '398135549725-pm9shjiu4fta240nirdbb8baihakf4ph.apps.googleusercontent.com', 'yOBwbclBDVDdB_hsr7PCezIH'

Choose a reason for hiding this comment

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

Metrics/LineLength: Line is too long. [136/120]

config.stretches = Rails.env.test? ? 1 : 11

# Set up a pepper to generate the hashed password.
# config.pepper = '9a0cd2508e19f24dcf762310977ed5b530c7e967d9f92c4011ddba30047ad115c5946236098878e9df43fded3ef3f027fb1e71b9954847666b0c24a722c77f60'

Choose a reason for hiding this comment

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

Metrics/LineLength: Line is too long. [150/120]

# confirmation, reset password and unlock tokens in the database.
# Devise will use the `secret_key_base` as its `secret_key`
# by default. You can change it below and use your own secret key.
# config.secret_key = 'f7ffe60b93af3e9eb4820b35e765b23edd5e61d53cf8c465737e0b1886c01b21748653f27cad511b9c0dd636a70336574f67a765fa22a35a9ef10dffc3b59248'

Choose a reason for hiding this comment

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

Metrics/LineLength: Line is too long. [154/120]

@@ -0,0 +1,64 @@
# frozen_string_literal: true

class SubjectsController < ApplicationController

Choose a reason for hiding this comment

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

Style/Documentation: Missing top-level class documentation comment.

def after_sign_in_path_for(user)
# binding.pry
subjects_url(user)
end

Choose a reason for hiding this comment

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

Layout/DefEndAlignment: end at 10, 1 is not aligned with def at 7, 2.

Copy link
Owner Author

Choose a reason for hiding this comment

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

fixed

@@ -0,0 +1,6 @@
body.controller-devise {
background-color: #f2f7ff;
background-image: url('./backgroundimage.jpeg');

Choose a reason for hiding this comment

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

Prefer double-quoted strings

@@ -0,0 +1,6 @@
body.controller-devise {
background-color: #f2f7ff;

Choose a reason for hiding this comment

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

Color literals like #f2f7ff should only be used in variable declarations; they should be referred to via variable everywhere else.

@@ -0,0 +1,6 @@
body.controller-devise {

Choose a reason for hiding this comment

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

Avoid qualifying class selectors with an element.

@mantasavas mantasavas merged commit bfd53df into master Apr 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants