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 << '' 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
  • #{select :item, related_fk, related.find(:all, :order => related.typus_order_by).collect { |p| [p.typus_name, p.id] }, { :include_blank => true }, { :disabled => attribute_disabled?(attribute) } }
  • 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
  • #{date_select :item, attribute, options, { :disabled => attribute_disabled?(attribute)} }
  • HTML end def typus_datetime_field(attribute, options) <<-HTML
  • #{datetime_select :item, attribute, options, {:disabled => attribute_disabled?(attribute)}}
  • HTML end # Optimize def typus_file_field(attribute) attachment = attribute.split('_file_name').first unless @item.send(attribute).blank? item = @item.send(attachment) preview = if @item.send("#{attachment}_content_type") =~ /^image\/.+/ && item.styles.member?(:typus_thumbnail) image_tag item.url(:typus_thumbnail) else link_to @item.send(attribute), item.url end end <<-HTML
  • #{file_field :item, attachment, :disabled => attribute_disabled?(attribute)} #{preview}
  • HTML end def typus_password_field(attribute) <<-HTML
  • #{password_field :item, attribute, :class => 'text', :disabled => attribute_disabled?(attribute)}
  • HTML end def typus_selector_field(attribute) returning(String.new) do |html| options = [] @resource[:class].send(attribute).each do |option| case option.kind_of?(Array) when true selected = (@item.send(attribute).to_s == option.last.to_s) ? 'selected' : '' options << "" else selected = (@item.send(attribute).to_s == option.to_s) ? 'selected' : '' options << "" end end html << <<-HTML
  • HTML end end def typus_text_field(attribute) <<-HTML
  • #{text_area :item, attribute, :class => 'text', :rows => @resource[:class].typus_options_for(:form_rows), :disabled => attribute_disabled?(attribute)}
  • HTML end def typus_tiny_mce_field(attribute) <<-HTML
  • #{text_area :item, attribute, :class => 'mceEditor', :disabled => attribute_disabled?(attribute)}
  • HTML end def typus_time_field(attribute, options) <<-HTML
  • #{time_select :item, attribute, options, {:disabled => attribute_disabled?(attribute)}}
  • 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
  • #{text_field :item, attribute, :class => 'text', :disabled => attribute_disabled?(attribute) }
  • 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

    #{link_to model_to_relate.typus_human_name.pluralize, { :controller => "admin/#{model_to_relate_as_resource}", foreign_key => @item.id }, :title => _("{{model}} filtered by {{filtered_by}}", :model => model_to_relate.typus_human_name.pluralize, :filtered_by => @item.typus_name)} #{link_to _("Add new"), link_options if @current_user.can_perform?(model_to_relate, 'create')}

    HTML conditions = if model_to_relate.typus_options_for(:only_user_items) && !@current_user.is_root? { Typus.user_fk => @current_user } end options = { :order => model_to_relate.typus_order_by, :conditions => conditions } items_count = @resource[:class].find(params[:id]).send(field).count(:conditions => conditions) items_per_page = model_to_relate.typus_options_for(:per_page).to_i @pager = ::Paginator.new(items_count, items_per_page) do |offset, per_page| options.merge!({:limit => per_page, :offset => offset}) items = @resource[:class].find(params[:id]).send(field).find(:all, options) end @items = @pager.page(params[:page]) unless @items.empty? options = { :back_to => "#{@back_to}##{field}", :resource => @resource[:self], :resource_id => @item.id } html << build_list(model_to_relate, model_to_relate.typus_fields_for(:relationship), @items, model_to_relate_as_resource, options, association) html << pagination(:anchor => model_to_relate.name.tableize) unless pagination.nil? 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_and_belongs_to_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 condition = !(@resource[:class].typus_user_id? && @current_user.id == @item.send(Typus.user_fk)) unless condition add_new = <<-HTML #{link_to _("Add new"), :controller => field, :action => 'new', :back_to => @back_to, :resource => @resource[:self], :resource_id => @item.id if @current_user.can_perform?(model_to_relate, 'create')} HTML end html << <<-HTML

    #{link_to model_to_relate.typus_human_name.pluralize, :controller => "admin/#{model_to_relate_as_resource}"} #{add_new}

    HTML items_to_relate = (model_to_relate.find(:all) - @item.send(field)) unless condition || items_to_relate.empty? html << <<-HTML #{form_tag :action => 'relate', :id => @item.id} #{hidden_field :related, :model, :value => model_to_relate}

    #{ select :related, :id, items_to_relate.collect { |f| [f.typus_name, f.id] }.sort_by { |e| e.first } }   #{submit_tag _("Add"), :class => 'button'}

    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

    #{link_to model_to_relate.typus_human_name, :controller => "admin/#{model_to_relate_as_resource}"}

    HTML items = Array.new items << @resource[:class].find(params[:id]).send(field) unless @resource[:class].find(params[:id]).send(field).nil? unless items.empty? options = { :back_to => @back_to, :resource => @resource[:self], :resource_id => @item.id } html << build_list(model_to_relate, model_to_relate.typus_fields_for(:relationship), items, model_to_relate_as_resource, options, association) else 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