Sha256: 06e37f1459baa2f0d09625a6d41bf05fd87f1467bc4a1c6aac8433e9dbfe7950

Contents?: true

Size: 1.77 KB

Versions: 3

Compression:

Stored size: 1.77 KB

Contents

module Admin::Resources::RelationshipsHelper

  def setup_relationship(field)
    @field = field
    @model_to_relate = @resource.reflect_on_association(field.to_sym).class_name.typus_constantize
    @model_to_relate_as_resource = @model_to_relate.to_resource
    @reflection = @resource.reflect_on_association(field.to_sym)
    @association_name = @reflection.name.to_s
  end

  def build_pagination
    items_per_page = @model_to_relate.typus_options_for(:per_page)
    data = @item.send(@field).order(@model_to_relate.typus_order_by).where(set_conditions)
    @items = data.page(params[:page]).per(items_per_page)
  end

  def build_relationship_table
    build_list(@model_to_relate,
               @model_to_relate.typus_fields_for(:relationship),
               @items,
               @model_to_relate_as_resource,
               {},
               @reflection.macro,
               @association_name)
  end

  def build_add_new(options = {})
    default_options = { :controller => "/admin/#{@model_to_relate.to_resource}",
                        :action => "index",
                        :resource => @resource.model_name,
                        :layout => 'admin/headless',
                        :resource_id => @item.id,
                        :resource_action => 'relate',
                        :return_to => request.path }

    if set_condition && admin_user.can?("create", @model_to_relate)
      link_to Typus::I18n.t("Add New"), default_options.merge(options), { :class => "iframe" }
    end
  end

  def set_condition
    (@resource.typus_user_id? && admin_user.is_not_root?) ? admin_user.owns?(@item) : true
  end

  def set_conditions
    if @model_to_relate.typus_options_for(:only_user_items) && admin_user.is_not_root?
      { Typus.user_foreign_key => admin_user }
    end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
typus-3.1.0.rc11 app/helpers/admin/resources/relationships_helper.rb
typus-3.1.0.rc10 app/helpers/admin/resources/relationships_helper.rb
typus-3.1.0.rc9 app/helpers/admin/resources/relationships_helper.rb