Class: Rails::Generators::ChannelGenerator
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
|
|
Instance Chain:
|
|
Inherits: |
Rails::Generators::NamedBase
|
Defined in: | actioncable/lib/rails/generators/channel/channel_generator.rb |
Class Attribute Summary
Base
- Inherited
Class Method Summary
NamedBase
- Inherited
.check_class_collision | Add a class collisions name to be checked on class initialization. |
.new |
Base
- Inherited
.base_root | Returns the base root for a common set of generators. |
.default_source_root | Returns the default source root for a given generator. |
.desc | Tries to get the description from a USAGE file one folder above the source root otherwise uses a default description. |
.hide! | Convenience method to hide this generator from the available ones when running rails generator command. |
.hook_for | Invoke a generator based on the value supplied by the user to the given option named “name”. |
.namespace | Convenience method to get the namespace from the class name. |
.remove_hook_for | Remove a previously added hook. |
.source_root | Returns the source root for this generator using default_source_root as default. |
.add_shebang_option! | Small macro to add ruby as an option to the generator with proper default value plus an instance helper method called shebang. |
.banner | Use Rails default banner. |
.base_name | Sets the base_name taking into account the current class namespace. |
.default_aliases_for_option | Returns default aliases for the option name given doing a lookup in aliases. |
.default_for_option | Returns default for the option name given doing a lookup in config. |
.default_generator_root, | |
.default_value_for_option | Returns the default value for the option name given doing a lookup in options. |
.generator_name | Removes the namespaces and get the generator name. |
.usage_path, | |
.class_option | |
.inherited | Cache source root and add lib/generators/base/generator/templates to source paths. |
.hooks | Keep hooks configuration that are used on prepare_for_invocation. |
.prepare_for_invocation | Prepare class invocation to search on |
Instance Attribute Summary
- #first_setup_required? ⇒ Boolean readonly private
- #using_bun? ⇒ Boolean readonly private
- #using_importmap? ⇒ Boolean readonly private
- #using_javascript? ⇒ Boolean readonly private
- #using_js_runtime? ⇒ Boolean readonly private
- #using_node? ⇒ Boolean readonly private
NamedBase
- Inherited
#file_name, #inside_template, #inside_template?, #mountable_engine?, #pluralize_table_names?, #uncountable? |
Base
- Inherited
Instance Method Summary
- #create_channel_files
- #create_channel_file private
- #create_channel_javascript_file private
- #create_shared_channel_files private
- #create_shared_channel_javascript_files private
- #file_name private
- #import_channel_in_javascript_entrypoint private
- #import_channels_in_javascript_entrypoint private
- #install_javascript_dependencies private
- #pin_javascript_dependencies private
- #root private
NamedBase
- Inherited
#application_name | Tries to retrieve the application name or simply return application. |
#assign_names!, #attributes_names, #class_name, #class_path, #edit_helper, #file_path, #fixture_file_name, #human_name, #i18n_scope, #index_helper, #model_resource_name, #namespaced_class_path, #new_helper, | |
#parse_attributes! | Convert attributes array into |
#plural_file_name, #plural_name, #plural_route_name, #plural_table_name, #redirect_resource_name, #regular_class_path, #route_url, #show_helper, | |
#singular_name | FIXME: We are avoiding to use alias because a bug on thor that make this method public and add it to the task list. |
#singular_route_name, #singular_table_name, #table_name, #url_helper_prefix |
Base
- Inherited
#class_collisions | Check whether the given class names are already taken by user application or Ruby on |
#extract_last_module | Takes in an array of nested modules and extracts the last module. |
#indent, | |
#module_namespacing | Wrap block with namespace of current application if namespace exists and is not skipped. |
#namespace, #namespace_dirs, #namespaced_path, #wrap_with_namespace |
Actions
- Included
#add_source | Add the given source to |
#application | Alias for Actions#environment. |
#environment | Adds configuration code to a Rails runtime environment. |
#gem | Adds a |
#gem_group | Wraps gem entries inside a group. |
#generate | Runs another generator. |
#git | Runs one or more git commands. |
#github, | |
#initializer | Creates an initializer file in |
#lib | Creates a file in |
#rails_command | Runs the specified Rails command. |
#rake | Runs the specified Rake task. |
#rakefile | Creates a Rake tasks file in |
#readme | Reads the given file at the source root and prints it in the console. |
#route | Make an entry in Rails routing file |
#vendor | Creates a file in |
#append_file_with_newline | Append string to a file with a newline if necessary. |
#execute_command | Runs the supplied command using either “rake …” or “rails …” based on the executor parameter provided. |
#indentation | Indent the |
#log | Define log for backwards compatibility. |
#match_file, | |
#optimize_indentation | Returns optimized string with indentation. |
#quote | Always returns value in double quotes. |
#rebase_indentation | Alias for Actions#optimize_indentation. |
#route_namespace_pattern, | |
#with_indentation | Manage |
#initialize |
Constructor Details
This class inherits a constructor from Rails::Generators::NamedBase
Instance Attribute Details
#first_setup_required? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'actioncable/lib/rails/generators/channel/channel_generator.rb', line 95
def first_setup_required? !root.join("app/javascript/channels/index.js").exist? end
#using_bun? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'actioncable/lib/rails/generators/channel/channel_generator.rb', line 107
def using_bun? # Cannot assume bun.lockb has been generated yet so we look for a file known to # be generated by the jsbundling-rails gem @using_bun ||= using_js_runtime? && root.join("bun.config.js").exist? end
#using_importmap? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'actioncable/lib/rails/generators/channel/channel_generator.rb', line 118
def using_importmap? @using_importmap ||= root.join("config/importmap.rb").exist? end
#using_javascript? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'actioncable/lib/rails/generators/channel/channel_generator.rb', line 99
def using_javascript? @using_javascript ||= [:assets] && root.join("app/javascript").exist? end
#using_js_runtime? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'actioncable/lib/rails/generators/channel/channel_generator.rb', line 103
def using_js_runtime? @using_js_runtime ||= root.join("package.json").exist? end
#using_node? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'actioncable/lib/rails/generators/channel/channel_generator.rb', line 113
def using_node? # Bun is the only runtime that _isn't_ node. @using_node ||= using_js_runtime? && !root.join("bun.config.js").exist? end
Instance Method Details
#create_channel_file (private)
[ GitHub ]#create_channel_files
[ GitHub ]# File 'actioncable/lib/rails/generators/channel/channel_generator.rb', line 18
def create_channel_files create_shared_channel_files create_channel_file if using_javascript? if first_setup_required? create_shared_channel_javascript_files import_channels_in_javascript_entrypoint if using_importmap? pin_javascript_dependencies elsif using_js_runtime? install_javascript_dependencies end end create_channel_javascript_file import_channel_in_javascript_entrypoint end end
#create_channel_javascript_file (private)
[ GitHub ]# File 'actioncable/lib/rails/generators/channel/channel_generator.rb', line 59
def create_channel_javascript_file channel_js_path = File.join("app/javascript/channels", class_path, "#{file_name}_channel") js_template "javascript/channel", channel_js_path gsub_file "#{channel_js_path}.js", /\.\/consumer/, "channels/consumer" unless using_js_runtime? end
#file_name (private)
[ GitHub ]# File 'actioncable/lib/rails/generators/channel/channel_generator.rb', line 91
def file_name @_file_name ||= super.sub(/_channel\z/i, "") end
#import_channel_in_javascript_entrypoint (private)
[ GitHub ]# File 'actioncable/lib/rails/generators/channel/channel_generator.rb', line 70
def import_channel_in_javascript_entrypoint append_to_file "app/javascript/channels/index.js", using_js_runtime? ? %(import "./#{file_name}_channel"\n) : %(import "channels/#{file_name}_channel"\n) end
#import_channels_in_javascript_entrypoint (private)
[ GitHub ]# File 'actioncable/lib/rails/generators/channel/channel_generator.rb', line 65
def import_channels_in_javascript_entrypoint append_to_file "app/javascript/application.js", using_js_runtime? ? %(import "./channels"\n) : %(import "channels"\n) end
#install_javascript_dependencies (private)
[ GitHub ]# File 'actioncable/lib/rails/generators/channel/channel_generator.rb', line 75
def install_javascript_dependencies say "Installing JavaScript dependencies", :green if using_bun? run "bun add @rails/actioncable" elsif using_node? run "yarn add @rails/actioncable" end end
#pin_javascript_dependencies (private)
[ GitHub ]# File 'actioncable/lib/rails/generators/channel/channel_generator.rb', line 84
def pin_javascript_dependencies append_to_file "config/importmap.rb", <<-RUBY pin "@rails/actioncable", to: "actioncable.esm.js" pin_all_from "app/javascript/channels", under: "channels" RUBY end