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

Url validation and Authorisation #48

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open

Url validation and Authorisation #48

wants to merge 25 commits into from

Conversation

Nimmyjv
Copy link
Contributor

@Nimmyjv Nimmyjv commented Apr 7, 2017

No description provided.

def destroy?
record.user == user
end
end

Choose a reason for hiding this comment

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

Indentation

@@ -21,3 +21,4 @@

en:
hello: "Hello world"

Choose a reason for hiding this comment

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

why?

Copy link
Contributor

Choose a reason for hiding this comment

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

Rubocop warns about not having newline at end of file. Either way, the last line shouldn't have the indentation.

@@ -19,6 +19,7 @@ def call
attr_reader :user, :collection

def delete_collection
Pundit.authorize(user, collection, :destroy?)
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be done in the controller, not here.

@@ -1,3 +1,4 @@
# frozen_string_literal: true
Copy link
Contributor

Choose a reason for hiding this comment

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

Not needed. Ignore this warning if rubocop shows it.

false
end

def valid_host? host
Copy link
Contributor

Choose a reason for hiding this comment

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

Parentheses around args.

Choose a reason for hiding this comment

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

Afaik rubocop suggests to not use parenthesis if the number of arguments are 1

Copy link
Contributor

Choose a reason for hiding this comment

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

I doubt that. Rubocop follows bbatsov's styleguide, which prefers parentheses all the time, and I don't remember seeing a warning about this even though I always use parens around single args.

Maybe you're thinking about the case where there are no args?

host.present? && valid_characters?(host)
end

def valid_characters? host
Copy link
Contributor

Choose a reason for hiding this comment

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

Parentheses around args.

class Website < ApplicationRecord
before_validation :add_protocol_to_website
validates :url, url: true
Copy link
Contributor

Choose a reason for hiding this comment

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

Group different types of statements (callbacks, validations, associations, etc) together, and separate them by empty lines.

@nithinbekal
Copy link
Contributor

@Nimmyjv When you submit a PR that fixes a specific issue, please add "fixes #issueNo" in the description. It will be automatically connected to it.

@@ -38,6 +40,6 @@ def fetch_meta_description
private

def add_protocol_to_website
self.url = (self.url[/^http:\/\//] || self.url[/^https:\/\//]) ? self.url : "http://#{self.url}"
self.url = url[/^http:\/\//] || url[/^https:\/\//] ? url : "http://#{url}"
Copy link
Contributor

Choose a reason for hiding this comment

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

This is difficult to read. In Leo, we rewrote it like this:

    def add_protocol_to_url
      return if protocol_present?
      self.url = "https://#{url}"
    end

    def protocol_present?
      return false if url.blank?
      url[/^http:\/\//] || url[/^https:\/\//]
    end

@@ -1,3 +1,4 @@

Copy link
Contributor

Choose a reason for hiding this comment

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

Blank line not needed.

@@ -1,6 +1,6 @@
# frozen_string_literal: true
Copy link
Contributor

Choose a reason for hiding this comment

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

As mentioned before, remove this and ignore frozen_string_literal warnings from rubocop. Also, edit rubocop.yml to ignore these warnings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

@@ -1,3 +1,4 @@
# frozen_string_literal: true
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove this.

@@ -7,7 +7,9 @@ def create
end

def destroy
Collections::Delete.call(params[:id], current_user)
collection = Collection.find(params[:id])
Pundit.authorize(current_user, collection, :destroy?)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is Pundit. required? authorize(...) works, I believe.

@@ -19,7 +19,7 @@ def call
attr_reader :user, :collection

def delete_collection
CollectionWebsite.where('collection_id = ?', collection.id).destroy_all
CollectionWebsite.where(collection_id: collection.id).destroy_all
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't this be achieved by doing dependent: destroy on has_many :collection_websites?

@nithinbekal
Copy link
Contributor

LGTM. 👍


def user_not_authorized
flash[:alert] = 'You are not authorized to perform this action.'
redirect_to(request.referer || root_path)

Choose a reason for hiding this comment

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

You can write this like redirect_back(fallback_location: root_path)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

…edpanthers.co is added it is stored as http://redpanthers.co

When again redpanthers.co added to the same list, it was added because it is compared with http://redpanthers.co
To prevent this, when url is added itself, it is checked whether starts with http or https. Else, http is added and then compared
@@ -1,4 +1,4 @@
$(document).ready(function() {
$(document).ready(function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Trailing space.

@@ -1,5 +1,4 @@
class Invite < ApplicationRecord
has_secure_token

Copy link
Contributor

Choose a reason for hiding this comment

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

Leave a blank line between different kind of declarations.

@nithinbekal
Copy link
Contributor

@Nimmyjv Are the tests running fine? I think these changes might be breaking some of the tests, or causing HTTP requests. If there are HTTP requests to 3rd parties, use webmock to stub out the requests.

@@ -1,5 +1,5 @@
class Invite < ApplicationRecord
has_secure_token

Copy link
Contributor

Choose a reason for hiding this comment

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

Trailing whitespace.


Copy link
Contributor

Choose a reason for hiding this comment

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

Trailing whitespace, and unnecessary 2nd blank line.

url
else
"http://#{url}"
end
Copy link
Contributor

Choose a reason for hiding this comment

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

Extract the condition out, so you can rewrite it like this:

url = "http://#{url}" unless protocol_given?(url)

db/seeds.rb Outdated
@@ -20,4 +25,4 @@
end
end

AdminUser.create!(email: '[email protected]', password: 'password', password_confirmation: 'password')
AdminUser.first_or_create!(email: '[email protected]', password: 'password', password_confirmation: 'password')
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really need first_or_create in seeds.rb? It is only run in an empty database, right?

db/seeds.rb Outdated

urls.each do |url|
user.websites.create!(url: url)
user = User.first_or_create!(name: 'abc', email: '[email protected]', password: '123456', password_confirmation: '123456')
Copy link
Contributor

Choose a reason for hiding this comment

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

Whitespace in the middle of line.

db/seeds.rb Outdated
collection = user.collections.create!(name: name)
urls.each do |url|
collection.websites.create!(url: url)
end
end
Copy link
Contributor

Choose a reason for hiding this comment

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

In seeds.rb, you don't really need all this complexity, since you know what data you want to add. You can write it like this:

user = User.create!(name: 'abc', email: '[email protected]', password: '123456', password_confirmation: '123456')
collection = user.collections.create!(name: 'First Collection')
collection.websites.create!(url: 'https://redpanthers.co')

When adding seed data, it would be good to add valid url, since you will be using it in development.

Websites::Create.call(params, user)

user.reload
assert_equal 1, user.site_number, 'site number'
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this field contain number of websites a user has? Then can we call it websites_count instead? This seems an awkward name for the field.

(Do this in a separate PR rather than mixing these.)


def protocol_given?(url)
url if (url.start_with? 'http://') || (url.start_with? 'https://')
end
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't return url. A method ending with ? should return true or false.

<% end %>
<% end %>
<% if collection_nil.empty? %>
<% @collection_names.any? %>
Copy link
Contributor

Choose a reason for hiding this comment

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

What does this do? It just seems to return true or false and render nothing.

@@ -11,7 +11,7 @@ class CreateTest < ActiveSupport::TestCase
Websites::Create.call(params, user)

user.reload
assert_equal 1, user.site_number, 'site number'
assert_equal 1, user.websites.count, 'websites_count'
Copy link
Contributor

Choose a reason for hiding this comment

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

I meant renaming site_number to websites_count since that is more accurately describing what it contains, not user.websites.count.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

okay

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants