<commit_before>module Admin::Resources::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_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 }

    # Add new basically means: We can create new items or relate existing ones.
    create_or_read = admin_user.can?("create", @model_to_relate) || admin_user.can?("read", @model_to_relate)

    if set_condition && create_or_read
      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
<commit_msg>Gardening<commit_after>module Admin::Resources::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_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 }

    # Add new basically means: We can create new items or relate existing ones.
    create_or_read = admin_user.can?("create", @model_to_relate) || admin_user.can?("read", @model_to_relate)

    if set_condition && create_or_read
      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

{'fraction_non_alphanumeric': 0.10904522613065326, 'fraction_numerical': 0.0010050251256281408, 'mean_word_length': 2.88390243902439, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3012844', 'n_tokens_mistral': 1299, 'n_tokens_neox': 1256, 'n_words': 190}