Skip to content

Commit

Permalink
Added record_for helper
Browse files Browse the repository at this point in the history
fixed the form_builder to work also on Ruby 1.9
  • Loading branch information
Bert Hajee authored and Bert Hajee committed Jul 21, 2013
1 parent 5c8ab24 commit 1ce9d22
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/page_record/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module PageRecord
class FormBuilder < ActionView::Helpers::FormBuilder
include PageRecord::Helpers

helpers = field_helpers + %w(time_zone_select date_select) - %w(hidden_field fields_for label)
helpers = field_helpers + [:time_zone_select, :date_select] - [:hidden_field, :fields_for, :label]
helpers.each do |helper|
define_method helper do |field, *args|
options = args.find { |a| a.is_a?(Hash) } || {}
Expand Down
29 changes: 29 additions & 0 deletions lib/page_record/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,35 @@ def form_record_for(type, var = nil)
Hash["data-#{type}-id", id]
end

##
#
# 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:
#
# ```ruby
# <tr <%= record_for(:team)%>>
# ```
# this returns the follwing HTML:
#
# ```html
# <tr data-team-id="2">
# ```
#
# @param type Symbol identifying the type of record and the variable to use
# @param var the variable to use. This is optional
#
# @return Hash
#
#
def record_for(record, type = nil)
unless type
type = record.class.to_s.downcase
end
"data-#{type}-id=#{record.id}"
end

##
#
# Returns a hash containing the attribute name. This can be used as html options in rails helpers
Expand Down

0 comments on commit 1ce9d22

Please sign in to comment.