Module: PageRecord::Helpers
- Included in:
- FormBuilder
- Defined in:
- lib/page_record/helpers.rb
Instance Method Summary collapse
-
#action_for(name) ⇒ Object
Returns a hash containing the action name.
-
#attribute_for(name) ⇒ Object
Returns a hash containing the attribute name.
-
#attribute_tag_for(name, attribute, content_or_options_with_block = nil, options = nil, escape = true, &block) ⇒ Object
(also: #atf)
Writes a tag containing the specified PageRecord attribute.
-
#error_for(name) ⇒ Object
Returns a hash containing the error name.
-
#form_record_for(type, var = nil) ⇒ Object
returns a hash containing the record-type and the id.
-
#record_for(record, type = nil) ⇒ Object
returns a string containing the record-type and the id.
-
#record_form_for(record, options = {}, &block) ⇒ Object
build a form that automagicaly identifies a form as a PageRecord recognisable form The form is identified by the id of the given record or
new
if the record is new.
Instance Method Details
#action_for(name) ⇒ Object
Returns a hash containing the action name. This can be used as html options in rails helpers
example in a form builder block:
<%= f.submit "Submit", action_for(:save)%>
this returns the follwing HTML:
<input data-action-for="submit" name="commit" type="submit" value="Submit">
114 115 116 117 118 |
# File 'lib/page_record/helpers.rb', line 114 def action_for(name) name = name.to_s name = 'save' if name == 'submit' Hash['data-action-for', name] end |
#attribute_for(name) ⇒ Object
Returns a hash containing the attribute name. This can be used as html options in rails helpers
example in a form builder block:
<%= f.text_field :name, attribute_for(:name) %>
this returns the follwing HTML:
<input data-attribute-for="name" id="team_name" name="team[name]" type="text">
89 90 91 |
# File 'lib/page_record/helpers.rb', line 89 def attribute_for(name) Hash['data-attribute-for', name] end |
#attribute_tag_for(name, attribute, content_or_options_with_block = nil, options = nil, escape = true, &block) ⇒ Object Also known as: atf
Writes a tag containing the specified PageRecord attribute
example:
<% @teams.each do |team| %>
<tr>
<%= attribute_tag_for(:td, :name) { team.name} %>
<%= attribute_tag_for(:td, :competition) { team.competition} %>
<%= attribute_tag_for(:td, :point) { team.point} %>
<%= attribute_tag_for(:td, :ranking) { team.ranking} %>
<td><%= link_to 'Show', team, action_for(:show) %></td>
<td><%= link_to 'Edit', edit_team_path(team), action_for(:edit) %></td>
<td><%= link_to 'Destroy', team, {method: :delete, data: { confirm: 'Are you sure?' }}.merge(action_for(:destroy)) %></td>
</tr>
the first attribute_tag_for
lines returns the follwing HTML:
<td data-attribute-for="name">aa</td>
rubocop:disable ParameterLists
171 172 173 174 175 176 177 178 179 |
# File 'lib/page_record/helpers.rb', line 171 def attribute_tag_for(name, attribute, = nil, = nil, escape = true, &block) ||= ? << { 'data-attribute-for' => attribute } : { 'data-attribute-for' => attribute } if block_given? = if .is_a?(Hash) content_tag_string(name, capture(&block), , escape) else content_tag_string(name, , , escape) end end |
#error_for(name) ⇒ Object
Returns a hash containing the error name. This can be used as html options in rails helpers
example:
TODO: Make an example
this returns the follwing HTML:
<div data-error-for="name">can't be blank</div>
140 141 142 |
# File 'lib/page_record/helpers.rb', line 140 def error_for(name) Hash['data-error-for', name] end |
#form_record_for(type, var = nil) ⇒ Object
returns a hash containing the record-type and the id. The id is based on the type. For example when
you specify :team
as the type, it will search for the @type
instance variable.
example:
<%= form_for(@team, html:form_record_for(:team)) do |f| %>
<% end %>
this returns the follwing HTML:
<form accept-charset="UTF-8" action="/teams/2" class="edit_team" data-team-id="2" id="edit_team_2" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value=""><input name="_method" type="hidden" value="patch"><input name="authenticity_token" type="hidden" value="QXIbPXH65Ek+8i0j2R8akdHX2WXLo2MuDFuUVL8CQpY="></div>
</form>
29 30 31 32 33 34 35 36 37 |
# File 'lib/page_record/helpers.rb', line 29 def form_record_for(type, var = nil) if var id = var.id else id = instance_eval("@#{type}.id") end id ||= 'new' Hash["data-#{type}-id", id] end |
#record_for(record, type = nil) ⇒ Object
returns a string containing the record-type and the id. The id is based on the type. For example when
you specify :team
as the type, it will search for the type
instance variable.
example:
<tr <%= record_for(:team)%>>
this returns the follwing HTML:
<tr data-team-id="2">
61 62 63 64 65 66 |
# File 'lib/page_record/helpers.rb', line 61 def record_for(record, type = nil) unless type type = record.class.to_s.downcase end "data-#{type}-id=#{record.id}" end |
#record_form_for(record, options = {}, &block) ⇒ Object
build a form that automagicaly identifies a form as a PageRecord recognisable form
The form is identified by the id of the given record or new
if the record is new.
All the elements in the form are labeled according to there field name. And thus easy recognisable by PageRecord
example:
<%= record_form_for(@team) do |f| %>
<div class="field" >
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field"%>
<%= f.label :competition %><br>
<%= f.text_field :competition%>
</div>
<div class="actions">
<%= f.submit "Submit"%>
</div>
<% end %>
214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/page_record/helpers.rb', line 214 def record_form_for(record, = {}, &block) case record when String, Symbol object_name = record else object = record.is_a?(Array) ? record.last : record raise ArgumentError, 'First argument in form cannot contain nil or be empty' unless object object_name = [:as] || model_name_from_record_or_class(object).param_key end = .merge(html: form_record_for(object_name), builder: PageRecord::FormBuilder) form_for(record, , &block) end |