Class: JsFromRoutes::Instance
- Inherits:
-
Object
- Object
- JsFromRoutes::Instance
- Defined in:
- lib/js_from_routes/generator.rb
Overview
Public: A generator instance with its own configuration. Allows generating routes separately for different sub-apps in a Rails monolith.
Example:
JsFromRoutes.config(:admin) do |config|
config.output_folder = Rails.root.join("app/javascript/admin/api")
config.export_if = ->(route) { route.defaults[:export] == :admin }
end
JsFromRoutes.config(:public) do |config|
config.output_folder = Rails.root.join("app/javascript/public/api")
config.export_if = ->(route) { route.defaults[:export] == :public }
end
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#generate!(app_or_routes = Rails.application) ⇒ Object
Public: Generates code for the specified routes with { export: true }.
-
#initialize(name, config) ⇒ Instance
constructor
A new instance of Instance.
Constructor Details
#initialize(name, config) ⇒ Instance
Returns a new instance of Instance.
183 184 185 186 |
# File 'lib/js_from_routes/generator.rb', line 183 def initialize(name, config) @name = name @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
181 182 183 |
# File 'lib/js_from_routes/generator.rb', line 181 def config @config end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
181 182 183 |
# File 'lib/js_from_routes/generator.rb', line 181 def name @name end |
Instance Method Details
#generate!(app_or_routes = Rails.application) ⇒ Object
Public: Generates code for the specified routes with { export: true }.
189 190 191 192 193 |
# File 'lib/js_from_routes/generator.rb', line 189 def generate!(app_or_routes = Rails.application) raise ArgumentError, "A Rails app must be defined, or you must specify a custom `output_folder`" if config.output_folder.to_s.blank? rails_routes = app_or_routes.is_a?(::Rails::Engine) ? app_or_routes.routes.routes : app_or_routes generate_files(exported_routes_by_controller(rails_routes)) end |