Merge pull request #1401 from Br3nda/fix/codestyle

Another code style fixup
This commit is contained in:
pozorvlak
2017-10-18 10:52:38 +01:00
committed by GitHub
4 changed files with 62 additions and 50 deletions

10
.hound.yml Normal file
View File

@@ -0,0 +1,10 @@
---
fail_on_violations: true
ruby:
config_file: .rubocop.yml
haml:
config_file: .haml-lint.yml
scss:
config_file: .scss-lint.yml
eslint:
config_file: .eslintrc

View File

@@ -28,7 +28,6 @@ Lint/UnusedBlockArgument:
Exclude:
- 'app/controllers/sessions_controller.rb'
- 'config/unicorn.rb'
- 'lib/haml/filters/growstuff_markdown.rb'
# Cop supports --auto-correct.
# Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods.
@@ -121,7 +120,6 @@ Style/ClassAndModuleChildren:
- 'app/controllers/admin/orders_controller.rb'
- 'lib/actions/oauth_signup_action.rb'
- 'lib/haml/filters/escaped_markdown.rb'
- 'lib/haml/filters/growstuff_markdown.rb'
# Cop supports --auto-correct.
Style/ColonMethodCall:
@@ -226,11 +224,6 @@ Style/PercentLiteralDelimiters:
- 'spec/features/signin_spec.rb'
- 'spec/features/signout_spec.rb'
# Cop supports --auto-correct.
Style/PerlBackrefs:
Exclude:
- 'lib/haml/filters/growstuff_markdown.rb'
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, AllowInnerSlashes.
# SupportedStyles: slashes, percent_r, mixed
@@ -256,11 +249,6 @@ Style/SymbolProc:
Exclude:
- 'lib/tasks/growstuff.rake'
# Cop supports --auto-correct.
Style/UnlessElse:
Exclude:
- 'app/controllers/omniauth_callbacks_controller.rb'
# Cop supports --auto-correct.
# Configuration parameters: SupportedStyles, WordRegex.
# SupportedStyles: percent, brackets

View File

@@ -29,15 +29,15 @@ class OmniauthCallbacksController < Devise::OmniauthCallbacksController
member = action.find_or_create_from_authorization(auth)
@authentication = action.establish_authentication(auth, member)
unless action.member_created?
sign_in_and_redirect member, event: :authentication # this will throw if @user is not activated
set_flash_message(:notice, :success, kind: auth['provider']) if is_navigational_format?
else
if action.member_created?
raise "Invalid provider" unless ['facebook', 'twitter', 'flickr'].index(auth['provider'].to_s)
session["devise.#{auth['provider']}_data"] = request.env["omniauth.auth"]
sign_in member
redirect_to finish_signup_url(member)
else
sign_in_and_redirect member, event: :authentication # this will throw if @user is not activated
set_flash_message(:notice, :success, kind: auth['provider']) if is_navigational_format?
end
end

View File

@@ -1,56 +1,70 @@
require 'bluecloth'
module Haml::Filters
module Haml::Filters # rubocop:disable Style/ClassAndModuleChildren
module GrowstuffMarkdown
include Haml::Filters::Base
def render(text)
@expanded = text
expand_crops!
expand_members!
BlueCloth.new(@expanded).to_html
end
private
CROP_REGEX = /(?<!\\)\[([^\[\]]+?)\]\(crop\)/
MEMBER_REGEX = /(?<!\\)\[([^\[\]]+?)\]\(member\)/
MEMBER_AT_REGEX = /(?<!\\)(\@\w+)/
MEMBER_ESCAPE_AT_REGEX = /(?<!\\)\\(?=\@\w+)/
include Haml::Filters::Base
HOST = Growstuff::Application.config.host
def render(text)
def expand_crops!
# turn [tomato](crop) into [tomato](http://growstuff.org/crops/tomato)
expanded = text.gsub(CROP_REGEX) do |m|
crop_str = $1
@expanded = @expanded.gsub(CROP_REGEX) do
crop_str = Regexp.last_match(1)
# find crop case-insensitively
crop = Crop.where('lower(name) = ?', crop_str.downcase).first
if crop
url = Rails.application.routes.url_helpers.crop_url(crop, host: Growstuff::Application.config.host)
"[#{crop_str}](#{url})"
else
crop_str
end
crop_link crop, crop_str
end
end
def expand_members!
# turn [jane](member) into [jane](http://growstuff.org/members/jane)
expanded = expanded.gsub(MEMBER_REGEX) do |m|
member_str = $1
# find member case-insensitively
member = Member.case_insensitive_login_name(member_str).first
if member
url = Rails.application.routes.url_helpers.member_url(member, only_path: true)
"[#{member_str}](#{url})"
else
member_str
end
end
# turn @jane into [@jane](http://growstuff.org/members/jane)
expanded = expanded.gsub(MEMBER_AT_REGEX) do |m|
member_str = $1
# find member case-insensitively
member = Member.case_insensitive_login_name(member_str[1..-1]).first
if member
url = Rails.application.routes.url_helpers.member_url(member, only_path: true)
"[#{member_str}](#{url})"
else
member_str
[MEMBER_REGEX, MEMBER_AT_REGEX].each do |re|
@expanded = @expanded.gsub(re) do
member_str = Regexp.last_match(1)
member = find_member(member_str)
member_link(member, member_str)
end
end
expanded = expanded.gsub(MEMBER_ESCAPE_AT_REGEX, "")
@expanded = @expanded.gsub(MEMBER_ESCAPE_AT_REGEX, '')
end
BlueCloth.new(expanded).to_html
def member_link(member, link_text)
if member
url = Rails.application.routes.url_helpers.member_url(member, only_path: true)
"[#{link_text}](#{url})"
else
link_text
end
end
def crop_link(crop, link_text)
if crop
url = Rails.application.routes.url_helpers.crop_url(crop, host: HOST)
"[#{link_text}](#{url})"
else
link_text
end
end
def find_member(login_name)
# Remove @ if present
login_name = login_name[1..-1] if login_name.start_with?('@')
Member.case_insensitive_login_name(login_name).first
end
end