Sha256: a9b7291c24fd3f75947305fee3bbe897e1d3b4354675212840034b96c726d84a

Contents?: true

Size: 1.57 KB

Versions: 12

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

module Phlex
	module Generators
		class ControllerGenerator < ::Rails::Generators::NamedBase # :nodoc:
			source_root File.expand_path("templates", __dir__)

			argument :actions,
				type: :array,
				default: [],
				banner: "action action"

			class_option :skip_routes,
				type: :boolean,
				desc: "Don't add routes to config/routes.rb."

			class_option :parent,
				type: :string,
				default: "ApplicationController",
				desc: "The parent class for the generated controller"

			check_class_collision suffix: "Controller"

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

			def copy_view_files
				base_path = File.join("app/views", class_path, file_name)
				empty_directory base_path

				actions.each do |action|
					::Rails::Generators.invoke("phlex:page", [name + "/" + action])
				end
			end

			def add_routes
				return if options[:skip_routes]

				routing_code = "resources :#{file_name}"

				if actions.any?
					routing_code << ", only: [#{actions.map { ":#{_1}" }.join(', ')}]"
				end

				route routing_code, namespace: regular_class_path
			end

			hook_for :test_framework, as: :controller do |generator|
				invoke generator, [remove_possible_suffix(name), actions]
			end

			private

			def parent_class_name
				options[:parent]
			end

			def file_name
				remove_possible_suffix(super)
			end

			def name
				remove_possible_suffix(super)
			end

			def remove_possible_suffix(name)
				name.sub(/_?controller$/i, "")
			end
		end
	end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
phlex-rails-0.6.1 lib/generators/phlex/controller/controller_generator.rb
phlex-rails-0.6.0 lib/generators/phlex/controller/controller_generator.rb
phlex-rails-0.5.1 lib/generators/phlex/controller/controller_generator.rb
phlex-rails-0.5.0 lib/generators/phlex/controller/controller_generator.rb
phlex-rails-0.4.2 lib/generators/phlex/controller/controller_generator.rb
phlex-rails-0.4.1 lib/generators/phlex/controller/controller_generator.rb
phlex-rails-0.4.0 lib/generators/phlex/controller/controller_generator.rb
phlex-rails-0.3.2 lib/generators/phlex/controller/controller_generator.rb
phlex-rails-0.3.1 lib/generators/phlex/controller/controller_generator.rb
phlex-rails-0.3.0 lib/generators/phlex/controller/controller_generator.rb
phlex-rails-0.2.2 lib/generators/phlex/controller/controller_generator.rb
phlex-rails-0.2.1 lib/generators/phlex/controller/controller_generator.rb