Sha256: b3dd6609e93e26276edbb4bc0f7927d8934d2c03faf0f8874185f9e6b2ef94e2

Contents?: true

Size: 1.97 KB

Versions: 13

Compression:

Stored size: 1.97 KB

Contents

module Admin::Resources::DataTypes::HasAndBelongsToManyHelper

  def table_has_and_belongs_to_many_field(attribute, item)
    item.send(attribute).map(&:to_label).join(", ")
  end

  alias_method :table_has_many_field, :table_has_and_belongs_to_many_field

  def typus_has_and_belongs_to_many_field(attribute, form)
    klass = @resource.reflect_on_association(attribute.to_sym).class_name.constantize

    resource_ids = "#{attribute.singularize}_ids"
    html_options = {}
    model = @resource.name.underscore.gsub("/", "_")
    options = { :attribute => "#{model}_#{attribute}" }

    label_text = @resource.human_attribute_name(attribute)
    if (text = build_label_text_for_has_and_belongs_to_many(klass, html_options, options))
      label_text += " <small>#{text}</small>"
    end

    locals = { :attribute => attribute,
               :attribute_id => "#{model}_#{attribute}",
               :related_klass => klass,
               :related_items => @item.send(attribute),
               :related_ids => "#{model}[#{resource_ids}][]",
               :values => klass,
               :form => form,
               :label_text => label_text.html_safe,
               :html_options => html_options }

    render "admin/templates/has_and_belongs_to_many", locals
  end

  def build_label_text_for_has_and_belongs_to_many(klass, html_options, options = {})
    if html_options[:disabled] == true
      Typus::I18n.t("Read only")
    elsif admin_user.can?('create', klass) && !headless_mode?
      build_add_new_for_has_and_belongs_to_many(klass, options)
    end
  end

  def build_add_new_for_has_and_belongs_to_many(klass, options)
    html_options = set_modal_options_for(klass)
    html_options["url"] = url_for(:controller => "/admin/#{klass.to_resource}", :action => :new, :_popup => true)
    html_options["data-controls-modal"] = "modal-from-dom-#{options[:attribute]}"

    options = { :anchor => html_options["data-controls-modal"] }

    link_to Typus::I18n.t("Add"), options, html_options
  end

end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
brainsome_typus-4.0.0.beta13 app/helpers/admin/resources/data_types/has_and_belongs_to_many_helper.rb
brainsome_typus-4.0.0.beta12 app/helpers/admin/resources/data_types/has_and_belongs_to_many_helper.rb
brainsome_typus-4.0.0.beta11 app/helpers/admin/resources/data_types/has_and_belongs_to_many_helper.rb
brainsome_typus-4.0.0.beta10 app/helpers/admin/resources/data_types/has_and_belongs_to_many_helper.rb
brainsome_typus-4.0.0.beta9 app/helpers/admin/resources/data_types/has_and_belongs_to_many_helper.rb
brainsome_typus-4.0.0.beta8 app/helpers/admin/resources/data_types/has_and_belongs_to_many_helper.rb
brainsome_typus-4.0.0.beta7 app/helpers/admin/resources/data_types/has_and_belongs_to_many_helper.rb
brainsome_typus-4.0.0.beta6 app/helpers/admin/resources/data_types/has_and_belongs_to_many_helper.rb
brainsome_typus-4.0.0.beta5 app/helpers/admin/resources/data_types/has_and_belongs_to_many_helper.rb
brainsome_typus-4.0.0.beta4 app/helpers/admin/resources/data_types/has_and_belongs_to_many_helper.rb
brainsome_typus-4.0.0.beta3 app/helpers/admin/resources/data_types/has_and_belongs_to_many_helper.rb
brainsome_typus-4.0.0.beta2 app/helpers/admin/resources/data_types/has_and_belongs_to_many_helper.rb
brainsome_typus-4.0.0.beta1 app/helpers/admin/resources/data_types/has_and_belongs_to_many_helper.rb