Sha256: f353a22b0065bcd7e73f4d9535fc683f75ca91ad1c16e89973134af755225c65

Contents?: true

Size: 1.68 KB

Versions: 28

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

require "rails/generators/named_base"
require "rails/generators/resource_helpers"

require_relative "../helpers/admin_generator_attributes"

module Koi
  class AdminControllerGenerator < Rails::Generators::NamedBase
    include Rails::Generators::ResourceHelpers
    include Helpers::AdminGeneratorAttributes

    source_root File.expand_path("templates", __dir__)

    check_class_collision prefix: "Admin::", suffix: "Controller"

    argument :attributes, type: :array, default: [], banner: "field:type field:type"

    def create_controller_files
      template("controller.rb",
               File.join("app/controllers",
                         controller_class_path,
                         "#{controller_file_name}_controller.rb"))
    end

    def create_spec_files
      template("controller_spec.rb",
               File.join("spec/requests",
                         controller_class_path,
                         "#{controller_file_name}_controller_spec.rb"))
    end

    hook_for(:admin_views, in: :koi, type: :boolean, default: true)
    hook_for(:admin_route, in: :koi, type: :boolean, default: true)

    private

    def controller_class_path
      ["admin"] + super
    end

    def permitted_params
      attachments, others = attributes_names.partition { |name| attachments?(name) }
      params              = others.map { |name| ":#{name}" }
      params += attachments.map { |name| "#{name}: []" }
      params.join(", ")
    end

    def attachments?(name)
      attribute = attributes.find { |attr| attr.name == name }
      attribute&.attachments?
    end

    def sort_attribute
      attributes.find { |attr| attr.type == :string }&.name
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
katalyst-koi-4.15.1 lib/generators/koi/admin_controller/admin_controller_generator.rb
katalyst-koi-4.15.0 lib/generators/koi/admin_controller/admin_controller_generator.rb
katalyst-koi-4.14.3 lib/generators/koi/admin_controller/admin_controller_generator.rb
katalyst-koi-4.14.2 lib/generators/koi/admin_controller/admin_controller_generator.rb
katalyst-koi-4.14.1 lib/generators/koi/admin_controller/admin_controller_generator.rb
katalyst-koi-4.14.0 lib/generators/koi/admin_controller/admin_controller_generator.rb
katalyst-koi-4.13.2 lib/generators/koi/admin_controller/admin_controller_generator.rb
katalyst-koi-4.13.0 lib/generators/koi/admin_controller/admin_controller_generator.rb
katalyst-koi-4.12.6 lib/generators/koi/admin_controller/admin_controller_generator.rb
katalyst-koi-4.12.5 lib/generators/koi/admin_controller/admin_controller_generator.rb
katalyst-koi-4.12.4 lib/generators/koi/admin_controller/admin_controller_generator.rb
katalyst-koi-4.12.3 lib/generators/koi/admin_controller/admin_controller_generator.rb
katalyst-koi-4.12.2 lib/generators/koi/admin_controller/admin_controller_generator.rb
katalyst-koi-4.12.1 lib/generators/koi/admin_controller/admin_controller_generator.rb
katalyst-koi-4.12.0 lib/generators/koi/admin_controller/admin_controller_generator.rb
katalyst-koi-4.11.2 lib/generators/koi/admin_controller/admin_controller_generator.rb
katalyst-koi-4.11.1 lib/generators/koi/admin_controller/admin_controller_generator.rb
katalyst-koi-4.11.0 lib/generators/koi/admin_controller/admin_controller_generator.rb
katalyst-koi-4.10.3 lib/generators/koi/admin_controller/admin_controller_generator.rb
katalyst-koi-4.10.2 lib/generators/koi/admin_controller/admin_controller_generator.rb