module Admin module RelationshipsHelper def setup_relationship(field) @field = field @model_to_relate = @resource.reflect_on_association(field.to_sym).class_name.constantize @model_to_relate_as_resource = @model_to_relate.to_resource @reflection = @resource.reflect_on_association(field.to_sym) @association = @reflection.macro end def typus_form_has_many(field) setup_relationship(field) foreign_key = @reflection.through_reflection ? @reflection.primary_key_name.pluralize : @reflection.primary_key_name @items_to_relate = @model_to_relate.send("find_all_by_#{foreign_key}", nil) if set_condition && !@items_to_relate.empty? form = build_relate_form end build_pagination options = { foreign_key => @item.id } render "admin/templates/has_n", :model_to_relate => @model_to_relate, :model_to_relate_as_resource => @model_to_relate_as_resource, :foreign_key => foreign_key, :add_new => raw(build_add_new(options)), :form => form, :table => build_relationship_table end def typus_form_has_and_belongs_to_many(field) setup_relationship(field) if @model_to_relate.count < 500 @items_to_relate = (@model_to_relate.all - @item.send(field)) if set_condition && !@items_to_relate.empty? form = build_relate_form end end build_pagination render "admin/templates/has_n", :model_to_relate => @model_to_relate, :model_to_relate_as_resource => @model_to_relate_as_resource, :add_new => raw(build_add_new), :form => form, :table => build_relationship_table end def build_pagination options = { :order => @model_to_relate.typus_order_by, :conditions => set_conditions } items_count = @resource.find(params[:id]).send(@field).count(:conditions => set_conditions) items_per_page = @model_to_relate.typus_options_for(:per_page) @pager = ::Paginator.new(items_count, items_per_page) do |offset, per_page| options.merge!({:limit => per_page, :offset => offset}) items = @resource.find(params[:id]).send(@field).all(options) end @items = @pager.page(params[:page]) end def build_relate_form render "admin/templates/relate_form", :model_to_relate => @model_to_relate, :items_to_relate => @items_to_relate end def build_relationship_table build_list(@model_to_relate, @model_to_relate.typus_fields_for(:relationship), @items, @model_to_relate_as_resource, {}, @association) end def build_add_new(options = {}) default_options = { :controller => @field, :action => "new", :resource => @resource.name, :resource_id => @item.id, :back_to => @back_to } return unless set_condition && @current_user.can?("create", @model_to_relate) link_to _("Add new"), default_options.merge(options) end def set_condition if @resource.typus_user_id? && @current_user.is_not_root? @item.owned_by?(@current_user) else true end end def set_conditions if @model_to_relate.typus_options_for(:only_user_items) && @current_user.is_not_root? { Typus.user_fk => @current_user } end end #-- # TODO: Move html code to partial. #++ def typus_form_has_one(field) html = "" model_to_relate = @resource.reflect_on_association(field.to_sym).class_name.constantize model_to_relate_as_resource = model_to_relate.to_resource reflection = @resource.reflect_on_association(field.to_sym) association = reflection.macro html << <<-HTML
#{message}