Skip to content

Commit

Permalink
Code tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Jul 7, 2016
1 parent 2ae6c00 commit 73cb06b
Show file tree
Hide file tree
Showing 27 changed files with 420 additions and 353 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Metrics/LineLength:
Enabled: false
Style/AndOr:
Enabled: false
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: ruby
sudo: false
cache: bundler
script: 'bundle exec rake test:coverage --trace'
script: 'bundle exec rake test:coverage --trace && bundle exec rubocop --display-cop-names'
rvm:
- 2.2.5
- 2.3.1
Expand Down
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ gem 'hanami-validations', '~> 0.6', github: 'hanami/validations', branch: '0.6.x
gem 'hanami-controller', '~> 0.7', github: 'hanami/controller', branch: '0.7.x'
gem 'hanami-view', '~> 0.7', github: 'hanami/view', branch: '0.7.x'

gem 'coveralls', require: false
gem 'rubocop', '~> 0.41', require: false
gem 'coveralls', require: false
7 changes: 4 additions & 3 deletions hanami-helpers.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ Gem::Specification.new do |spec|
spec.version = Hanami::Helpers::VERSION
spec.authors = ['Luca Guidi', 'Trung Lê', 'Alfonso Uceda']
spec.email = ['me@lucaguidi.com', 'trung.le@ruby-journal.com', 'uceda73@gmail.com']
spec.summary = %q{Hanami helpers}
spec.description = %q{View helpers for Ruby applications}
spec.summary = 'Hanami helpers'
spec.description = 'View helpers for Ruby applications'
spec.homepage = 'http://hanamirb.org'
spec.license = 'MIT'

spec.files = `git ls-files -- lib/* CHANGELOG.md LICENSE.md README.md hanami-helpers.gemspec`.split($/)
spec.files = `git ls-files -- lib/* CHANGELOG.md LICENSE.md README.md hanami-helpers.gemspec`.split($/) # rubocop:disable Style/SpecialGlobalVars

spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']
Expand Down
2 changes: 1 addition & 1 deletion lib/hanami-helpers.rb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require 'hanami/helpers'
require 'hanami/helpers' # rubocop:disable Style/FileName
9 changes: 5 additions & 4 deletions lib/hanami/helpers/escape_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Hanami
module Helpers
# Escape helpers
#
# You can include this module inside your view and
# You can include this module inside your view and
# the view will have access all methods.
#
# By including <tt>Hanami::Helpers::EscapeHelper</tt> it will inject private
Expand All @@ -13,6 +13,7 @@ module Helpers
# @since 0.1.0
module EscapeHelper
private

# Escape the given HTML tag content.
#
# This should be used only for untrusted contents: user input.
Expand Down Expand Up @@ -75,7 +76,7 @@ def escape_html(input)
end

# @since 0.1.0
alias_method :h, :escape_html
alias h escape_html

# Escape the given HTML tag attribute.
#
Expand Down Expand Up @@ -147,7 +148,7 @@ def escape_html_attribute(input)
end

# @since 0.1.0
alias_method :ha, :escape_html_attribute
alias ha escape_html_attribute

# Escape an URL to be used in HTML attributes
#
Expand Down Expand Up @@ -225,7 +226,7 @@ def escape_url(input, schemes = Utils::Escape::DEFAULT_URL_SCHEMES)
end

# @since 0.1.0
alias_method :hu, :escape_url
alias hu escape_url

# Bypass escape.
#
Expand Down
12 changes: 6 additions & 6 deletions lib/hanami/helpers/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,13 @@ def verb
# # </form>
def form_for(name, url, options = {}, &blk)
form = if name.is_a?(Form)
options = url
name
else
Form.new(name, url, options.delete(:values))
end
options = url
name
else
Form.new(name, url, options.delete(:values))
end

attributes = { action: form.url, method: form.verb, :'accept-charset' => DEFAULT_CHARSET, id: "#{ form.name }-form" }.merge(options)
attributes = { action: form.url, method: form.verb, :'accept-charset' => DEFAULT_CHARSET, id: "#{form.name}-form" }.merge(options)
FormBuilder.new(form, attributes, self, &blk)
end

Expand Down
34 changes: 18 additions & 16 deletions lib/hanami/helpers/form_helper/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ module FormHelper
# @since 0.2.0
#
# @see Hanami::Helpers::HtmlHelper::HtmlBuilder
class FormBuilder < ::Hanami::Helpers::HtmlHelper::HtmlBuilder
class FormBuilder < ::Hanami::Helpers::HtmlHelper::HtmlBuilder # rubocop:disable Metrics/ClassLength
# Set of HTTP methods that are understood by web browsers
#
# @since 0.2.0
# @api private
BROWSER_METHODS = ['GET', 'POST'].freeze
BROWSER_METHODS = %w(GET POST).freeze

# Set of HTTP methods that should NOT generate CSRF token
#
# @since 0.2.0
# @api private
EXCLUDED_CSRF_METHODS = ['GET'].freeze
EXCLUDED_CSRF_METHODS = %w(GET).freeze

# Checked attribute value
#
Expand Down Expand Up @@ -104,7 +104,7 @@ class FormBuilder < ::Hanami::Helpers::HtmlHelper::HtmlBuilder
#
# @since 0.2.0
# @api private
def initialize(form, attributes, context = nil, &blk)
def initialize(form, attributes, context = nil, &blk) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
super()

@context = context
Expand Down Expand Up @@ -363,7 +363,7 @@ def label(content, attributes = {})
# # <input type="checkbox" name="book[languages][]" value="italian" checked="checked">
# # <input type="checkbox" name="book[languages][]" value="english">
def check_box(name, attributes = {})
_hidden_field_for_check_box( name, attributes)
_hidden_field_for_check_box(name, attributes)
input _attributes_for_check_box(name, attributes)
end

Expand Down Expand Up @@ -621,7 +621,7 @@ def text_area(name, content = nil, attributes = {})
content = nil
end

attributes = {name: _input_name(name), id: _input_id(name)}.merge(attributes)
attributes = { name: _input_name(name), id: _input_id(name) }.merge(attributes)
textarea(content || _value(name), attributes)
end

Expand All @@ -643,7 +643,7 @@ def text_area(name, content = nil, attributes = {})
def text_field(name, attributes = {})
input _attributes(:text, name, attributes)
end
alias_method :input_text, :text_field
alias input_text text_field

# Radio input
#
Expand Down Expand Up @@ -780,7 +780,7 @@ def password_field(name, attributes = {})
# # <option value="it" selected="selected">Italy</option>
# # <option value="us">United States</option>
# # </select>
def select(name, values, attributes = {})
def select(name, values, attributes = {}) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
options = attributes.delete(:options) { {} }
attributes = { name: _input_name(name), id: _input_id(name) }.merge(attributes)
prompt = options.delete(:prompt)
Expand All @@ -791,9 +791,9 @@ def select(name, values, attributes = {})

values.each do |content, value|
if selected == value || _value(name) == value
option(content, {value: value, selected: SELECTED}.merge(options))
option(content, { value: value, selected: SELECTED }.merge(options))
else
option(content, {value: value}.merge(options))
option(content, { value: value }.merge(options))
end
end
end
Expand Down Expand Up @@ -863,7 +863,7 @@ def select(name, values, attributes = {})
# # <option value="Italy" class="form-control"></option>
# # <option value="United States" class="form-control"></option>
# # </datalist>
def datalist(name, values, list, attributes = {})
def datalist(name, values, list, attributes = {}) # rubocop:disable Metrics/MethodLength
attrs = attributes.dup
options = attrs.delete(:options) || {}
datalist = attrs.delete(:datalist) || {}
Expand All @@ -874,7 +874,7 @@ def datalist(name, values, list, attributes = {})
text_field(name, attrs)
super(datalist) do
values.each do |value, content|
option(content, {value: value}.merge(options))
option(content, { value: value }.merge(options))
end
end
end
Expand All @@ -900,6 +900,7 @@ def submit(content, attributes = {})
end

protected

# A set of options to pass to the sub form helpers.
#
# @api private
Expand All @@ -909,6 +910,7 @@ def options
end

private

# Check the current builder is top-level
#
# @api private
Expand Down Expand Up @@ -958,7 +960,7 @@ def _attributes(type, name, attributes)
# @api private
# @since 0.2.0
def _input_name(name)
"#{ @name }[#{ name }]"
"#{@name}[#{name}]"
end

# Input <tt>id</tt> HTML attribute
Expand Down Expand Up @@ -1000,11 +1002,11 @@ def _for(content, name)
# @see Hanami::Helpers::FormHelper::FormBuilder#check_box
def _hidden_field_for_check_box(name, attributes)
if attributes[:value].nil? || !attributes[:unchecked_value].nil?
input({
input(
type: :hidden,
name: attributes[:name] || _input_name(name),
value: attributes.delete(:unchecked_value) || DEFAULT_UNCHECKED_VALUE
})
)
end
end

Expand All @@ -1024,7 +1026,7 @@ def _attributes_for_check_box(name, attributes)

value = _value(name)
attributes[:checked] = CHECKED if !value.nil? &&
( value == attributes[:value] || value.include?(attributes[:value]) )
(value == attributes[:value] || value.include?(attributes[:value]))

attributes
end
Expand Down
5 changes: 3 additions & 2 deletions lib/hanami/helpers/form_helper/html_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def initialize(name, content, attributes, options)
end

private

# Resolve the (nested) content
#
# @return [String] the content
Expand Down Expand Up @@ -68,9 +69,9 @@ def _method_override!
def _csrf_protection!
return if @csrf_token.nil?

_csrf_token = @csrf_token
csrf_token = @csrf_token
@builder.resolve do
input(type: :hidden, name: FormHelper::CSRF_TOKEN, value: _csrf_token)
input(type: :hidden, name: FormHelper::CSRF_TOKEN, value: csrf_token)
end
end
end
Expand Down
26 changes: 21 additions & 5 deletions lib/hanami/helpers/form_helper/values.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,47 @@
module Hanami
module Helpers
module FormHelper
# Values from params and form helpers.
#
# It's responsible to populate input values with data coming from params
# and inline values specified via form helpers like `text_field`.
#
# @since 0.2.0
# @api private
class Values
# @since 0.2.0
# @api private
GET_SEPARATOR = '.'.freeze

# @since 0.2.0
# @api private
def initialize(values, params)
@values = Utils::Hash.new(values).stringify!
@params = params
end

# Returns the value for the given key, if present
#
# @since 0.2.0
# @api private
def get(key)
@params.get(key) || _get_from_values(key)
end

private

# @since 0.2.0
# @api private
def _get_from_values(key)
initial_key, *keys = key.to_s.split(GET_SEPARATOR)
result = @values[initial_key]

Array(keys).each do |k|
break if result.nil?

result = if result.respond_to?(k)
result.public_send(k)
else
nil
end
result = if result.respond_to?(k) # rubocop:disable Style/IfUnlessModifier
result.public_send(k)
end
end

result
Expand Down
1 change: 1 addition & 0 deletions lib/hanami/helpers/html_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ module Helpers
# # </div>
module HtmlHelper
private

# Instantiate an HTML builder
#
# @param blk [Proc,Hanami::Helpers::HtmlHelper::HtmlBuilder,NilClass] the optional content block
Expand Down
Loading

0 comments on commit 73cb06b

Please sign in to comment.