Sha256: 9a358662c8eb382d5487800d8d65f423075364fc8204273e1148c997625f0624

Contents?: true

Size: 1.55 KB

Versions: 3

Compression:

Stored size: 1.55 KB

Contents

module ActiveAdminAddons
  module InputMethods
    def model_name
      valid_object.class.to_s.underscore.tr('/', '_')
    end

    def valid_method
      raise "invalid method given" if method.blank?
      method
    end

    def valid_object
      raise "blank object given" if @object.blank?
      @object
    end

    def object_class
      valid_object.class
    end

    def association_name
      valid_method.to_s.singularize.chomp("_id")
    end

    def method_model
      @options[:method_model] ||
        object_class.reflect_on_association(association_name).try(:klass) ||
        association_name.classify.constantize
    end

    def tableize_method
      association_name.tableize
    end

    def input_related_items
      valid_object.send(tableize_method)
    rescue NoMethodError
      raise "no association called #{tableize_method} on #{model_name} model"
    end

    def input_value
      @input_value ||= valid_object.send(valid_method)
    end

    def translated_method
      valid_object.class.human_attribute_name(valid_method)
    end

    def url_from_method
      url = ["/"]

      if ActiveAdmin.application.default_namespace.present?
        url << "#{ActiveAdmin.application.default_namespace}/"
      end

      url << tableize_method
      url.join("")
    end

    def build_virtual_attr
      attribute_name = "virtual_#{valid_method}_attr"
      fail "#{attribute_name} is already defined" if valid_object.respond_to?(attribute_name)
      valid_object.singleton_class.send(:attr_accessor, attribute_name)
      attribute_name
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
activeadmin_addons-1.8.3 lib/activeadmin_addons/support/input_helpers/input_methods.rb
activeadmin_addons-1.8.2 lib/activeadmin_addons/support/input_helpers/input_methods.rb
activeadmin_addons_rails6-1.7.2 lib/activeadmin_addons/support/input_helpers/input_methods.rb