Skip to content

Commit

Permalink
[#994] deunderscore_attributes for use of text_field_tag etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanlarsen committed Jan 11, 2012
1 parent 5c6e000 commit 4f5ca18
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
8 changes: 6 additions & 2 deletions dryml/lib/dryml/template_environment.rb
Expand Up @@ -105,6 +105,10 @@ def attrs_for(name)
end


def deunderscore_attributes(attrs)
HashWithIndifferentAccess[attrs.map{ |attr, value| [attr.to_s.gsub('_', '-'), value]}]
end

def add_classes!(attributes, *classes)
classes = classes.flatten.select{|x|x}
current = attributes[:class]
Expand All @@ -118,8 +122,8 @@ def add_classes(attributes, *classes)
end

def add_data_rapid!(attrs, tag, options)
data_rapid = ActiveSupport::JSON.decode(attrs["data-rapid"] || attrs["data_rapid"] || "{}")
attrs["data-rapid"] = data_rapid.update(tag => options).to_json
data_rapid = ActiveSupport::JSON.decode(attrs["data_rapid"] || "{}")
attrs["data_rapid"] = data_rapid.update(tag => options).to_json
attrs
end

Expand Down
24 changes: 12 additions & 12 deletions dryml/taglibs/core.dryml
Expand Up @@ -5,7 +5,7 @@
-->
<def tag="call-tag" attrs="tag">
<%= Dryml.static_tags.include?(tag) ?
content_tag(tag, parameters.default, attributes) :
content_tag(tag, parameters.default, deunderscore_attributes(attributes)) :
send(tag.gsub('-', '_'), attributes, parameters) %>
</def>

Expand All @@ -25,7 +25,7 @@ For example, you might want to wrap an `<img>` tag in an `<a>` tag but only unde
<% parameter ||= :default %>
<%= if when_
if Dryml.static_tags.include?(tag)
content_tag(tag, parameters.default, attributes)
content_tag(tag, parameters.default, deunderscore_attributes(attributes))
else
send(tag.gsub('-', '_'), attributes, { parameter.to_sym => parameters[:default] })
end
Expand Down Expand Up @@ -115,32 +115,32 @@ collection is considered blank)


<!-- nodoc. Define core HTML tags defined in Rapid so that DRYML can be used without Rapid. -->
<def tag="html"><%=raw "<html#{tag_options(attributes, true)}>" -%><do param="default"/><%= raw "</html>" -%></def>
<def tag="html"><%=raw "<html#{tag_options(deunderscore_attributes(attributes), true)}>" -%><do param="default"/><%= raw "</html>" -%></def>

<!-- nodoc. Define core HTML tags defined in Rapid so that DRYML can be used without Rapid. -->
<def tag="table"><%=raw "<table#{tag_options(attributes, true)}>" -%><do param="default"/><%= raw "</table>" -%></def>
<def tag="table"><%=raw "<table#{tag_options(deunderscore_attributes(attributes), true)}>" -%><do param="default"/><%= raw "</table>" -%></def>

<!-- nodoc. Define core HTML tags defined in Rapid so that DRYML can be used without Rapid. -->
<def tag="a"><%=raw "<a#{tag_options(attributes, true)}>" -%><do param="default"/><%= raw "</a>" -%></def>
<def tag="a"><%=raw "<a#{tag_options(deunderscore_attributes(attributes), true)}>" -%><do param="default"/><%= raw "</a>" -%></def>

<!-- nodoc. Define core HTML tags defined in Rapid so that DRYML can be used without Rapid. -->
<def tag="section"><%=raw "<section#{tag_options(attributes, true)}>" -%><do param="default"/><%= raw "</section>" -%></def>
<def tag="section"><%=raw "<section#{tag_options(deunderscore_attributes(attributes), true)}>" -%><do param="default"/><%= raw "</section>" -%></def>

<!-- nodoc. Define core HTML tags defined in Rapid so that DRYML can be used without Rapid. -->
<def tag="header"><%=raw "<header#{tag_options(attributes, true)}>" -%><do param="default"/><%= raw "</header>" -%></def>
<def tag="header"><%=raw "<header#{tag_options(deunderscore_attributes(attributes), true)}>" -%><do param="default"/><%= raw "</header>" -%></def>

<!-- nodoc. Define core HTML tags defined in Rapid so that DRYML can be used without Rapid. -->
<def tag="footer"><%=raw "<footer#{tag_options(attributes, true)}>" -%><do param="default"/><%= raw "</footer>" -%></def>
<def tag="footer"><%=raw "<footer#{tag_options(deunderscore_attributes(attributes), true)}>" -%><do param="default"/><%= raw "</footer>" -%></def>

<!-- nodoc. Define core HTML tags defined in Rapid so that DRYML can be used without Rapid. -->
<def tag="form"><%=raw "<form#{tag_options(attributes, true)}>" -%><do param="default"/><%= raw "</form>" -%></def>
<def tag="form"><%=raw "<form#{tag_options(deunderscore_attributes(attributes), true)}>" -%><do param="default"/><%= raw "</form>" -%></def>

<!-- nodoc. Define core HTML tags defined in Rapid so that DRYML can be used without Rapid. -->
<def tag="submit"><%=raw "<submit#{tag_options(attributes, true)}>" -%><do param="default"/><%= raw "</submit>" -%></def>
<def tag="submit"><%=raw "<submit#{tag_options(deunderscore_attributes(attributes), true)}>" -%><do param="default"/><%= raw "</submit>" -%></def>

<!-- nodoc. Define core HTML tags defined in Rapid so that DRYML can be used without Rapid. -->
<def tag="input"><%=raw "<input#{tag_options(attributes, true)}>" -%><do param="default"/><%= raw "</input>" -%></def>
<def tag="input"><%=raw "<input#{tag_options(deunderscore_attributes(attributes), true)}>" -%><do param="default"/><%= raw "</input>" -%></def>

<!-- nodoc. Define core HTML tags defined in Rapid so that DRYML can be used without Rapid. -->
<def tag="link"><%=raw "<link#{tag_options(attributes, true)}>" -%><do param="default"/><%= raw "</link>" -%></def>
<def tag="link"><%=raw "<link#{tag_options(deunderscore_attributes(attributes), true)}>" -%><do param="default"/><%= raw "</link>" -%></def>

20 changes: 10 additions & 10 deletions hobo/lib/hobo/rapid/taglibs/rapid_forms.dryml
Expand Up @@ -298,7 +298,7 @@ If the context is a `has_many :through` association, the polymorphic `<collectio
-->
<def tag="input" attrs="no-edit"><%=
if attributes[:type]
element :input, attributes, nil, true, true
element :input, deunderscore_attributes(attributes), nil, true, true
else
no_edit ||= :view
no_edit = no_edit.to_sym
Expand Down Expand Up @@ -350,21 +350,21 @@ edit collections a `Category` model in your application:

<!-- A `<textarea>` input -->
<def tag="input" for="text" attrs="name">
<%= text_area_tag(name, this, attributes) %>
<%= text_area_tag(name, this, deunderscore_attributes(attributes)) %>
</def>

<!-- A checkbox plus a hidden-field. The hidden field trick comes from Rails - it means that when the checkbox is not checked, the parameter name is still submitted, with a '0' value (the value is '1' when the checkbox is checked) -->
<def tag="input" for="boolean" attrs="name">
<%= unless attributes[:disabled]
cb_tag = check_box_tag(name, '1', this, attributes)
cb_tag = check_box_tag(name, '1', this, deunderscore_attributes(attributes))
cb_hidden_tag = hidden_field_tag(name, '0', :id => nil)
cb_hidden_tag + cb_tag
end %>
</def>

<!-- A password input - `<input type='password'>` -->
<def tag="input" for="password" attrs="name">
<%= password_field_tag(name, this, attributes) %>
<%= password_field_tag(name, this, deunderscore_attributes(attributes)) %>
</def>

<!-- A date picker, using the `select_date` helper from Rails
Expand Down Expand Up @@ -444,22 +444,22 @@ The menus default to the current time if the current value is nil.

<!-- An `<input type='text'>` input. -->
<def tag="input" for="integer" attrs="name">
<%= text_field_tag(name, this, attributes) %>
<%= text_field_tag(name, this, deunderscore_attributes(attributes)) %>
</def>

<!-- An `<input type='text'>` input. -->
<def tag="input" for="BigDecimal" attrs="name">
<%= text_field_tag(name, this, attributes) %>
<%= text_field_tag(name, this, deunderscore_attributes(attributes)) %>
</def>

<!-- An `<input type='text'>` input. -->
<def tag="input" for="float" attrs="name">
<%= text_field_tag(name, this, attributes) %>
<%= text_field_tag(name, this, deunderscore_attributes(attributes)) %>
</def>

<!-- An `<input type='text'>` input. -->
<def tag="input" for="string" attrs="name">
<%= text_field_tag(name, this, attributes) %>
<%= text_field_tag(name, this, deunderscore_attributes(attributes)) %>
</def>

<!-- A `<select>` menu containing the values of an 'enum string'.
Expand Down Expand Up @@ -603,12 +603,12 @@ All the standard ajax attributes *except the callbacks* are supported (see the m
if in_place == false
attributes[:confirm] = confirm if confirm
attributes[:method] = :delete
button_to(label, url, attributes)
button_to(label, url, deunderscore_attributes(attributes))
else
fade = true if fade.nil?
attributes[:value] = label
attributes[:onclick] = "Hobo.removeButton(this, '#{url}', #{js_updates(update)}, {fade:#{fade}, confirm: '#{confirm}'})"
element(:input, attributes, nil, true, true)
element(:input, deunderscore_attributes(attributes), nil, true, true)
end
end
else
Expand Down
2 changes: 1 addition & 1 deletion hobo/lib/hobo/rapid/taglibs/rapid_pages.dryml
Expand Up @@ -207,7 +207,7 @@ See the source for the actual output
<!-- Simple wrapper for the `stylesheet_link_tag` helper. The `name` attribute can be a comma-separated list of stylesheet names.
-->
<def tag="stylesheet" attrs="name">
<%= stylesheet_link_tag *(comma_split(name) + [attributes]) %>
<%= stylesheet_link_tag *(comma_split(name) + [deunderscore_attributes(attributes)]) %>
</def>


Expand Down

0 comments on commit 4f5ca18

Please sign in to comment.