module Admin::FormHelper
def build_form(fields)
options = { :start_year => @resource[:class].typus_options_for(:start_year),
:end_year => @resource[:class].typus_options_for(:end_year),
:minute_step => @resource[:class].typus_options_for(:minute_step) }
returning(String.new) do |html|
html << (error_messages_for :item, :header_tag => 'h3')
html << '
'
fields.each do |key, value|
if template = @resource[:class].typus_template(key)
html << typus_template_field(key, template, options)
next
end
case value
when :belongs_to then html << typus_belongs_to_field(key)
when :boolean then html << typus_boolean_field(key)
when :date then html << typus_date_field(key, options)
when :datetime then html << typus_datetime_field(key, options)
when :file then html << typus_file_field(key)
when :password then html << typus_password_field(key)
when :selector then html << typus_selector_field(key)
when :text then html << typus_text_field(key)
when :tiny_mce then html << typus_tiny_mce_field(key)
when :time then html << typus_time_field(key, options)
when :tree then html << typus_tree_field(key)
else
html << typus_string_field(key)
end
end
html << '
'
end
end
def typus_belongs_to_field(attribute)
##
# We only can pass parameters to 'new' and 'edit', so this hack makes
# the work to replace the current action.
#
params[:action] = (params[:action] == 'create') ? 'new' : params[:action]
back_to = '/' + [ params[:controller], params[:action], params[:id] ].compact.join('/')
related = @resource[:class].reflect_on_association(attribute.to_sym).class_name.constantize
related_fk = @resource[:class].reflect_on_association(attribute.to_sym).primary_key_name
message = [ _("Are you sure you want to leave this page?"),
_("If you have made any changes to the fields without clicking the Save/Update entry button, your changes will be lost."),
_("Click OK to continue, or click Cancel to stay on this page.") ]
returning(String.new) do |html|
if related.respond_to?(:roots)
html << typus_tree_field(related_fk, related.roots, related_fk)
else
html << <<-HTML
HTML
end
end
end
def typus_boolean_field(attribute)
attribute_name = attribute.gsub(/\?$/,'')
custom_true = @resource[:class].typus_boolean(attribute)[:true]
custom_true = custom_true != 'True' ? custom_true : "Checked if active"
<<-HTML
#{check_box :item, attribute_name}
HTML
end
def typus_date_field(attribute, options)
<<-HTML
HTML
end
def typus_tree_field(attribute, items = @resource[:class].roots, attribute_virtual = 'parent_id')
<<-HTML
HTML
end
def typus_string_field(attribute)
# Read only fields.
if @resource[:class].typus_field_options_for(:read_only).include?(attribute)
value = 'read_only' if %w( edit ).include?(params[:action])
end
# Auto generated fields.
if @resource[:class].typus_field_options_for(:auto_generated).include?(attribute)
value = 'auto_generated' if %w( new edit ).include?(params[:action])
end
comment = %w( read_only auto_generated ).include?(value) ? "#{value} field".humanize : ''
attribute_humanized = @resource[:class].human_attribute_name(attribute)
attribute_humanized += " (#{attribute})" if attribute.include?('_id')
<<-HTML
HTML
end
def typus_relationships
@back_to = '/' + [ params[:controller], params[:action], params[:id] ].compact.join('/')
returning(String.new) do |html|
@resource[:class].typus_defaults_for(:relationships).each do |relationship|
association = @resource[:class].reflect_on_association(relationship.to_sym)
next if !@current_user.can_perform?(association.class_name.constantize, 'read')
case association.macro
when :has_and_belongs_to_many
html << typus_form_has_and_belongs_to_many(relationship)
when :has_many
html << typus_form_has_many(relationship)
when :has_one
html << typus_form_has_one(relationship)
end
end
end
end
def typus_form_has_many(field)
returning(String.new) do |html|
model_to_relate = @resource[:class].reflect_on_association(field.to_sym).class_name.constantize
model_to_relate_as_resource = model_to_relate.name.tableize
reflection = @resource[:class].reflect_on_association(field.to_sym)
association = reflection.macro
foreign_key = reflection.through_reflection ? reflection.primary_key_name.pluralize : reflection.primary_key_name
link_options = { :controller => "admin/#{model_to_relate_as_resource.pluralize}",
:action => 'new',
:back_to => "#{@back_to}##{field}",
:resource => @resource[:self].singularize,
:resource_id => @item.id,
foreign_key => @item.id }
html << <<-HTML
HTML
end
items = @resource[:class].find(params[:id]).send(field)
unless items.empty?
html << build_list(model_to_relate,
model_to_relate.typus_fields_for(:relationship),
items,
model_to_relate_as_resource,
{},
association)
else
html << <<-HTML
#{_("There are no {{records}}.", :records => model_to_relate.typus_human_name.pluralize.downcase)}
HTML
end
html << <<-HTML
HTML
end
end
def typus_form_has_one(field)
returning(String.new) do |html|
model_to_relate = @resource[:class].reflect_on_association(field.to_sym).class_name.constantize
model_to_relate_as_resource = model_to_relate.name.tableize
reflection = @resource[:class].reflect_on_association(field.to_sym)
association = reflection.macro
html << <<-HTML
#{_("There is no {{records}}.", :records => model_to_relate.typus_human_name.downcase)}
HTML
end
html << <<-HTML
HTML
end
end
def typus_template_field(attribute, template, options = {})
folder = Typus::Configuration.options[:templates_folder]
template_name = File.join(folder, template)
output = render(:partial => template_name, :locals => { :resource => @resource, :attribute => attribute, :options => options } )
output || "#{attribute}: Can not find the template '#{template}'"
end
def attribute_disabled?(attribute)
accessible = @resource[:class].accessible_attributes
return accessible.nil? ? false : !accessible.include?(attribute)
end
##
# Tree builder when model +acts_as_tree+
#
def expand_tree_into_select_field(items, attribute)
returning(String.new) do |html|
items.each do |item|
html << %{\n}
html << expand_tree_into_select_field(item.children, attribute) unless item.children.empty?
end
end
end
end