Skip to content

Commit

Permalink
feat(inputs): adds semantic classes on inputs and actions
Browse files Browse the repository at this point in the history
  • Loading branch information
abe33 committed Jan 19, 2014
1 parent 88a2129 commit 19ae750
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
:ruby
attrs = {
type: action,
name: name,
class: "#{builder.model_name} #{action}",
}

%button{ type: action, name: name }= builder.action_label(action)
%button.action{attrs}= builder.action_label(action)
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
attrs = {
name: builder.association_name(attribute_name),
id: builder.input_id(attribute_name),
multiple: multiple
class: "#{builder.model_name} #{attribute_name} #{input_html.delete(:class)}",
multiple: multiple,
}

%select{attrs}
%select.input{attrs}
- if defined?(include_blank) && include_blank
%option{value: ''}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
name: builder.input_name(attribute_name),
id: builder.input_id(attribute_name),
type: :checkbox,
class: "#{builder.model_name} #{attribute_name} #{input_html.delete(:class)}",
value: '1',
checked: object.try(attribute_name),
}
if defined? input_html
attrs.merge! builder.filter_attributes_for(:input, input_html)
end
attrs.merge! builder.filter_attributes_for(:input, input_html)

%input{attrs}
%input.input{attrs}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
name: builder.collection_name(attribute_name),
id: id,
type: :checkbox,
class: "#{builder.model_name} #{attribute_name} #{input_html.delete(:class)}",
value: value,
checked: selected.include?(value),
}
if defined? input_html
attrs.merge! builder.filter_attributes_for(:input, input_html)
end
attrs.merge! builder.filter_attributes_for(:input, input_html)

%p
%input{attrs}
%input.input{attrs}
%label{for: id}= label || value.to_s.humanize
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
attrs = {
name: builder.input_name(attribute_name),
id: builder.input_id(attribute_name),
class: "#{builder.model_name} #{attribute_name} #{input_html.delete(:class)}",
type: type == :string ? :text : type,
value: object.try(attribute_name),
}
if defined? input_html
attrs.merge! builder.filter_attributes_for(:input, input_html)
end
attrs.merge! builder.filter_attributes_for(:input, input_html)

%input{attrs}
%input.input{attrs}
7 changes: 3 additions & 4 deletions app/views/awesome_form/default_theme/inputs/_radios.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
name: builder.input_name(attribute_name),
id: id,
type: :radio,
class: "#{builder.model_name} #{attribute_name} #{input_html.delete(:class)}",
value: value,
checked: selected.include?(value),
}
if defined? input_html
attrs.merge! builder.filter_attributes_for(:input, input_html)
end
attrs.merge! builder.filter_attributes_for(:input, input_html)

%p
%input{attrs}
%input.input{attrs}
%label{for: id}= label || value.to_s.humanize
7 changes: 3 additions & 4 deletions app/views/awesome_form/default_theme/inputs/_select.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
attrs = {
name: multiple ? builder.collection_name(attribute_name) : builder.input_name(attribute_name),
id: builder.input_id(attribute_name),
class: "#{builder.model_name} #{attribute_name} #{input_html.delete(:class)}",
multiple: multiple
}
if defined? input_html
attrs.merge! builder.filter_attributes_for(:select, input_html)
end
attrs.merge! builder.filter_attributes_for(:select, input_html)

%select{attrs}
%select.input{attrs}
- if defined?(include_blank) && include_blank
%option{value: ''}

Expand Down
7 changes: 3 additions & 4 deletions app/views/awesome_form/default_theme/inputs/_text.html.haml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
:ruby
attrs = {
name: builder.input_name(attribute_name),
class: "#{builder.model_name} #{attribute_name} #{input_html.delete(:class)}",
id: builder.input_id(attribute_name),
}
if defined? input_html
attrs.merge! builder.filter_attributes_for(:textarea, input_html)
end
attrs.merge! builder.filter_attributes_for(:textarea, input_html)

%textarea{attrs}= object.try(attribute_name)
%textarea.input{attrs}= object.try(attribute_name)
10 changes: 7 additions & 3 deletions lib/awesome_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ def filter_attributes_for(html, options)
end
end

protected

def model_name
object.class.name.underscore.pluralize
object.class.name.underscore
end

def resource_name
model_name.pluralize
end

protected

def filter_arguments(*args)
options = {}

Expand Down
8 changes: 4 additions & 4 deletions lib/awesome_form/methods/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def partial_paths_for_action(action_options)
action = action_options[:action]

[
"awesome_form/actions/#{model_name}/#{action}",
"awesome_form/#{theme}/actions/#{model_name}/#{action}",
"awesome_form/actions/#{resource_name}/#{action}",
"awesome_form/#{theme}/actions/#{resource_name}/#{action}",

"awesome_form/actions/#{action}",
"awesome_form/#{theme}/actions/#{action}",
Expand All @@ -65,8 +65,8 @@ def partial_paths_for_action_wrapper(action_options)
action = action_options[:action]

[
"awesome_form/wrappers/#{model_name}/#{action}",
"awesome_form/#{theme}/wrappers/#{model_name}/#{action}",
"awesome_form/wrappers/#{resource_name}/#{action}",
"awesome_form/#{theme}/wrappers/#{resource_name}/#{action}",

"awesome_form/wrappers/#{action}",
"awesome_form/#{theme}/wrappers/#{action}",
Expand Down
7 changes: 5 additions & 2 deletions lib/awesome_form/methods/inputs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def options_for_input(attribute, options)
}
.merge(type_options)
.merge(options)
.reverse_merge({
input_html: {}
})
end

def default_inputs_content(options)
Expand All @@ -44,8 +47,8 @@ def partial_paths_for_input(input_options, scope=:inputs)
theme = AwesomeForm.theme

[
"awesome_form/#{scope}/#{model_name}/#{input_options[:attribute_name]}",
"awesome_form/#{theme}/#{scope}/#{model_name}/#{input_options[:attribute_name]}",
"awesome_form/#{scope}/#{resource_name}/#{input_options[:attribute_name]}",
"awesome_form/#{theme}/#{scope}/#{resource_name}/#{input_options[:attribute_name]}",

"awesome_form/#{scope}/#{input_options[:type]}",
"awesome_form/#{theme}/#{scope}/#{input_options[:type]}",
Expand Down
4 changes: 2 additions & 2 deletions lib/awesome_form/methods/labels.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module Methods
module Labels

def input_label(attribute_name)
I18n.t("awesome_form.labels.#{model_name}.#{attribute_name}", default: attribute_name.to_s.humanize)
I18n.t("awesome_form.labels.#{resource_name}.#{attribute_name}", default: attribute_name.to_s.humanize)
end

def action_label(action)
I18n.t("awesome_form.actions.#{model_name}.#{action}", {
I18n.t("awesome_form.actions.#{resource_name}.#{action}", {
default: I18n.t("awesome_form.actions.#{action}", {
default: action.to_s.humanize
})
Expand Down
14 changes: 14 additions & 0 deletions spec/features/classes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'spec_helper'

feature 'semantic classes' do
scenario 'on a form' do
visit '/users/new'

match_content_of(page, '
form
input.input.user.email
button.action.user.submit
')
end
end

0 comments on commit 19ae750

Please sign in to comment.