Sha256: d8eab5bf9d95e0065b2c94f5a7977328c85c12f4a217d0536a6e85db5cbae7c2

Contents?: true

Size: 1.45 KB

Versions: 3

Compression:

Stored size: 1.45 KB

Contents

module CrowdaiAdmin
  class CustomBuilder
    attr_accessor :context, :model, :args, :block

    def initialize(context, model, *args, &block)
      @context = context
      @model = model
      @args = *args
      @block = block
    end

    def render
      raise NotImplementedError
    end

    def self.create_view_methods
      builder_class = self
      builder_name = builder_method_name

      ::ActiveAdmin::Views::TableFor.class_eval do
        define_method("#{builder_name}_column") do |*args, &block|
          column(*args) { |model| builder_class.new(self, model, *args, &block).render }
        end
      end

      ::ActiveAdmin::Views::AttributesTable.class_eval do
        define_method("#{builder_name}_row") do |*args, &block|
          row(*args) { |model| builder_class.new(self, model, *args, &block).render }
        end
      end
    end

    def self.builder_method_name
      name.underscore.to_s.split("/").last.chomp("_builder")
    end

    protected

    def data
      @data ||= block ? block.call(model) : model.send(attribute)
    end

    def options
      @options ||= has_opts? ? args.last : {}
    end

    def class_name
      model.class.name.demodulize.underscore
    end

    def attribute
      @attribute ||= has_label? ? args[1] : args[0]
    end

    def has_label?
      has_opts? ? args.length == 3 : args.length == 2
    end

    def has_opts?
      @has_opts ||= args.last.is_a?(Hash)
    end

    create_view_methods
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
crowdai_admin-1.0.2 lib/crowdai_admin/custom_builder.rb
crowdai_admin-1.0.1 lib/crowdai_admin/custom_builder.rb
crowdai_admin-1.0.0 lib/crowdai_admin/custom_builder.rb