Module: PageRecord::Helpers

Included in:
FormBuilder
Defined in:
lib/page_record/helpers.rb

Instance Method Summary collapse

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">

Parameters:

  • name

    Symbol or String identifying the action name

Returns:

  • Hash



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">

Parameters:

  • name

    Symbol or String identifying the name

Returns:

  • Hash



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, content_or_options_with_block = nil, options = nil, escape = true, &block)
  options ||= options ? options << { 'data-attribute-for' => attribute } : { 'data-attribute-for' => attribute }
  if block_given?
    options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
    (name, capture(&block), options, escape)
  else
    (name, content_or_options_with_block, options, 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>

Parameters:

  • name

    Symbol or String identifying the name

Returns:

  • Hash



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>

Parameters:

  • type

    Symbol identifying the type of record and the variable to use

  • var (defaults to: nil)

    the variable to use. This is optional

Returns:

  • Hash



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">

Parameters:

  • type (defaults to: nil)

    Symbol identifying the type of record and the variable to use

  • var

    the variable to use. This is optional

Returns:

  • Hash



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 %>

Returns:

  • formBuilder object

See Also:



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, options = {}, &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 = options[:as] || model_name_from_record_or_class(object).param_key
  end
  options = options.merge(html: form_record_for(object_name), builder: PageRecord::FormBuilder)
  form_for(record, options, &block)
end