CINXE.COM

Rails::Generators::Actions

<!DOCTYPE html> <html lang="en"> <head> <title>Rails::Generators::Actions</title> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width,initial-scale=1"> <link rel="stylesheet" href="../../../css/reset.css" type="text/css" media="screen" /> <link rel="stylesheet" href="../../../css/panel.css" type="text/css" media="screen" /> <link rel="stylesheet" href="../../../css/main.css" type="text/css" media="screen" /> <link rel="stylesheet" href="../../../css/github.css" type="text/css" media="screen" /> <script src="../../../js/jquery-3.5.1.min.js" type="text/javascript" charset="utf-8"></script> <script src="../../../js/main.js" type="text/javascript" charset="utf-8"></script> <script src="../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script> <script src="../../../js/turbolinks.js" type="text/javascript" charset="utf-8"></script> <script src="../../../js/search_index.js" type="text/javascript" charset="utf-8"></script> <script src="../../../js/searcher.js" type="text/javascript" charset="utf-8"></script> <script src="../../../panel/tree.js" type="text/javascript" charset="utf-8"></script> <script src="../../../js/searchdoc.js" type="text/javascript" charset="utf-8"></script> <meta name="data-rel-prefix" content="../../../"> <meta name="data-tree-keys" content='["Rails", "Generators", "Actions"]'> <meta property="og:title" value="Rails::Generators::Actions"> <meta name="keywords" content="Rails::Generators::Actions class, gem, gem_group, github, add_source, environment, application, git, vendor, lib, rakefile, initializer, generate, rake, rails_command, route, readme, log, execute_command, quote, optimize_indentation, indentation, with_indentation"> </head> <body> <a class="sr-only sr-only-focusable" href="#content" data-turbolinks="false">Skip to Content</a> <a class="sr-only sr-only-focusable" href="#search" data-turbolinks="false">Skip to Search</a> <input type="checkbox" id="hamburger" class="panel_checkbox"> <label class="panel_mobile_button" for="hamburger"><span></span> Menu</label> <nav class="panel panel_tree" id="panel" data-turbolinks-permanent> <div class="header"> <input type="text" placeholder="Search (/) for a class, method, ..." autosave="searchdoc" results="10" id="search" autocomplete="off" tabindex="-1" /> <label class="panel_mobile_button_close" for="hamburger"><span></span> Close</label> </div> <div class="tree"> <ul> </ul> </div> <div class="result"> <ul> </ul> </div> <a href="links.html" id="links">index</a> </nav> <div class="banner"> <span>Ruby on Rails 8.0.0</span><br /> <h2> <span class="type">Module</span> Rails::Generators::Actions </h2> <ul class="files"> <li><a href="../../../files/railties/lib/rails/generators/actions_rb.html">railties/lib/rails/generators/actions.rb</a></li> <li><a href="../../../files/railties/lib/rails/generators/actions/create_migration_rb.html">railties/lib/rails/generators/actions/create_migration.rb</a></li> <li><a href="../../../files/railties/lib/rails/generators/migration_rb.html">railties/lib/rails/generators/migration.rb</a></li> </ul> <div id="version-badge">v8.0.0</div> </div> <main id="bodyContent"> <div id="content"> <!-- Method ref --> <div class="sectiontitle">Methods</div> <dl class="methods"> <dt>A</dt> <dd> <ul> <li> <a href="#method-i-add_source">add_source</a>, </li> <li> <a href="#method-i-application">application</a> </li> </ul> </dd> <dt>E</dt> <dd> <ul> <li> <a href="#method-i-environment">environment</a>, </li> <li> <a href="#method-i-execute_command">execute_command</a> </li> </ul> </dd> <dt>G</dt> <dd> <ul> <li> <a href="#method-i-gem">gem</a>, </li> <li> <a href="#method-i-gem_group">gem_group</a>, </li> <li> <a href="#method-i-generate">generate</a>, </li> <li> <a href="#method-i-git">git</a>, </li> <li> <a href="#method-i-github">github</a> </li> </ul> </dd> <dt>I</dt> <dd> <ul> <li> <a href="#method-i-indentation">indentation</a>, </li> <li> <a href="#method-i-initializer">initializer</a> </li> </ul> </dd> <dt>L</dt> <dd> <ul> <li> <a href="#method-i-lib">lib</a>, </li> <li> <a href="#method-i-log">log</a> </li> </ul> </dd> <dt>O</dt> <dd> <ul> <li> <a href="#method-i-optimize_indentation">optimize_indentation</a> </li> </ul> </dd> <dt>Q</dt> <dd> <ul> <li> <a href="#method-i-quote">quote</a> </li> </ul> </dd> <dt>R</dt> <dd> <ul> <li> <a href="#method-i-rails_command">rails_command</a>, </li> <li> <a href="#method-i-rake">rake</a>, </li> <li> <a href="#method-i-rakefile">rakefile</a>, </li> <li> <a href="#method-i-readme">readme</a>, </li> <li> <a href="#method-i-route">route</a> </li> </ul> </dd> <dt>V</dt> <dd> <ul> <li> <a href="#method-i-vendor">vendor</a> </li> </ul> </dd> <dt>W</dt> <dd> <ul> <li> <a href="#method-i-with_indentation">with_indentation</a> </li> </ul> </dd> </dl> <!-- Methods --> <h2 class="sectiontitle">Instance Public methods</h2> <div class="method"> <h3 class="title method-title" id="method-i-add_source"> <b>add_source</b>(source, options = {}, &amp;block) <a href="../../../classes/Rails/Generators/Actions.html#method-i-add_source" name="method-i-add_source" class="permalink">Link</a> </h3> <div class="description"> <p>Add the given source to <code>Gemfile</code></p> <p>If block is given, gem entries in block are wrapped into the source group.</p> <pre><code>add_source &quot;http://gems.github.com/&quot; add_source &quot;http://gems.github.com/&quot; do gem &quot;rspec-rails&quot; end </code></pre> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-add_source_source')" id="l_method-i-add_source_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/railties/lib/rails/generators/actions.rb#L151" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-add_source_source" class="dyn-source"> <pre><span class="ruby-comment"># File railties/lib/rails/generators/actions.rb, line 151</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">add_source</span>(<span class="ruby-identifier">source</span>, <span class="ruby-identifier">options</span> = {}, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-identifier">log</span> <span class="ruby-value">:source</span>, <span class="ruby-identifier">source</span> <span class="ruby-identifier">in_root</span> <span class="ruby-keyword">do</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">block</span> <span class="ruby-identifier">append_file_with_newline</span> <span class="ruby-string">&quot;Gemfile&quot;</span>, <span class="ruby-node">&quot;\nsource #{quote(source)} do&quot;</span>, <span class="ruby-value">force:</span> <span class="ruby-keyword">true</span> <span class="ruby-identifier">with_indentation</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-identifier">append_file_with_newline</span> <span class="ruby-string">&quot;Gemfile&quot;</span>, <span class="ruby-string">&quot;end&quot;</span>, <span class="ruby-value">force:</span> <span class="ruby-keyword">true</span> <span class="ruby-keyword">else</span> <span class="ruby-identifier">prepend_file</span> <span class="ruby-string">&quot;Gemfile&quot;</span>, <span class="ruby-node">&quot;source #{quote(source)}\n&quot;</span>, <span class="ruby-value">verbose:</span> <span class="ruby-keyword">false</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-application"> <b>application</b>(data = nil, options = {}) <a href="../../../classes/Rails/Generators/Actions.html#method-i-application" name="method-i-application" class="permalink">Link</a> </h3> <div class="description"> </div> <div class="aka"> Alias for: <a href="Actions.html#method-i-environment">environment</a> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-environment"> <b>environment</b>(data = nil, options = {}) <a href="../../../classes/Rails/Generators/Actions.html#method-i-environment" name="method-i-environment" class="permalink">Link</a> </h3> <div class="description"> <p>Adds configuration code to a Rails runtime environment.</p> <p>By default, adds code inside the <code>Application</code> class in <code>config/application.rb</code> so that it applies to all environments.</p> <pre><code>environment %(config.asset_host = &quot;cdn.provider.com&quot;) </code></pre> <p>Results in:</p> <pre><code># config/application.rb class Application &lt; Rails::Application config.asset_host = &quot;cdn.provider.com&quot; # ... end </code></pre> <p>If the <code>:env</code> option is specified, the code will be added to the corresponding file in <code>config/environments</code> instead.</p> <pre><code>environment %(config.asset_host = &quot;localhost:3000&quot;), env: &quot;development&quot; </code></pre> <p>Results in:</p> <pre><code># config/environments/development.rb Rails.application.configure do config.asset_host = &quot;localhost:3000&quot; # ... end </code></pre> <p><code>:env</code> can also be an array. In which case, the code is added to each corresponding file in <code>config/environments</code>.</p> <p>The code can also be specified as the return value of the block:</p> <pre><code>environment do %(config.asset_host = &quot;cdn.provider.com&quot;) end environment(nil, env: &quot;development&quot;) do %(config.asset_host = &quot;localhost:3000&quot;) end </code></pre> </div> <div class="aka"> Also aliased as: <a href="Actions.html#method-i-application">application</a> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-environment_source')" id="l_method-i-environment_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/railties/lib/rails/generators/actions.rb#L206" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-environment_source" class="dyn-source"> <pre><span class="ruby-comment"># File railties/lib/rails/generators/actions.rb, line 206</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">environment</span>(<span class="ruby-identifier">data</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-identifier">options</span> = {}) <span class="ruby-identifier">sentinel</span> = <span class="ruby-string">&quot;class Application &lt; Rails::Application\n&quot;</span> <span class="ruby-identifier">env_file_sentinel</span> = <span class="ruby-string">&quot;Rails.application.configure do\n&quot;</span> <span class="ruby-identifier">data</span> <span class="ruby-operator">||=</span> <span class="ruby-keyword">yield</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span> <span class="ruby-identifier">in_root</span> <span class="ruby-keyword">do</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:env</span>].<span class="ruby-identifier">nil?</span> <span class="ruby-identifier">inject_into_file</span> <span class="ruby-string">&quot;config/application.rb&quot;</span>, <span class="ruby-identifier">optimize_indentation</span>(<span class="ruby-identifier">data</span>, <span class="ruby-value">4</span>), <span class="ruby-value">after:</span> <span class="ruby-identifier">sentinel</span>, <span class="ruby-value">verbose:</span> <span class="ruby-keyword">false</span> <span class="ruby-keyword">else</span> <span class="ruby-constant">Array</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:env</span>]).<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">env</span><span class="ruby-operator">|</span> <span class="ruby-identifier">inject_into_file</span> <span class="ruby-node">&quot;config/environments/#{env}.rb&quot;</span>, <span class="ruby-identifier">optimize_indentation</span>(<span class="ruby-identifier">data</span>, <span class="ruby-value">2</span>), <span class="ruby-value">after:</span> <span class="ruby-identifier">env_file_sentinel</span>, <span class="ruby-value">verbose:</span> <span class="ruby-keyword">false</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-gem"> <b>gem</b>(*args) <a href="../../../classes/Rails/Generators/Actions.html#method-i-gem" name="method-i-gem" class="permalink">Link</a> </h3> <div class="description"> <p>Adds a <code>gem</code> declaration to the <code>Gemfile</code> for the specified gem.</p> <pre><code>gem &quot;rspec&quot;, group: :test gem &quot;technoweenie-restful-authentication&quot;, lib: &quot;restful-authentication&quot;, source: &quot;http://gems.github.com/&quot; gem &quot;rails&quot;, &quot;3.0&quot;, git: &quot;https://github.com/rails/rails&quot; gem &quot;RedCloth&quot;, &quot;&gt;= 4.1.0&quot;, &quot;&lt; 4.2.0&quot; gem &quot;rspec&quot;, comment: &quot;Put this comment above the gem declaration&quot; </code></pre> <p>Note that this method only adds the gem to the <code>Gemfile</code>; it does not install the gem.</p> <h4 id="method-i-gem-label-Options">Options</h4> <dl class="rdoc-list label-list"><dt><code>:version</code> <dd> <p>The version constraints for the gem, specified as a string or an array of strings:</p> <pre><code>gem &quot;my_gem&quot;, version: &quot;~&gt; 1.1&quot; gem &quot;my_gem&quot;, version: [&quot;&gt;= 1.1&quot;, &quot;&lt; 2.0&quot;] </code></pre> <p>Alternatively, can be specified as one or more arguments following the gem name:</p> <pre><code>gem &quot;my_gem&quot;, &quot;&gt;= 1.1&quot;, &quot;&lt; 2.0&quot; </code></pre> </dd><dt><code>:comment</code> <dd> <p>Outputs a comment above the <code>gem</code> declaration in the <code>Gemfile</code>.</p> <pre><code>gem &quot;my_gem&quot;, comment: &quot;First line.\nSecond line.&quot; </code></pre> <p>Outputs:</p> <pre><code># First line. # Second line. gem &quot;my_gem&quot; </code></pre> </dd><dt><code>:group</code> <dd> <p>The gem group in the <code>Gemfile</code> that the gem belongs to.</p> </dd><dt><code>:git</code> <dd> <p>The URL of the git repository for the gem.</p> </dd></dl> <p>Any additional options passed to this method will be appended to the <code>gem</code> declaration in the <code>Gemfile</code>. For example:</p> <pre><code>gem &quot;my_gem&quot;, comment: &quot;Edge my_gem&quot;, git: &quot;https://example.com/my_gem.git&quot;, branch: &quot;master&quot; </code></pre> <p>Outputs:</p> <pre><code># Edge my_gem gem &quot;my_gem&quot;, git: &quot;https://example.com/my_gem.git&quot;, branch: &quot;master&quot; </code></pre> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-gem_source')" id="l_method-i-gem_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/railties/lib/rails/generators/actions.rb#L67" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-gem_source" class="dyn-source"> <pre><span class="ruby-comment"># File railties/lib/rails/generators/actions.rb, line 67</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">gem</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>) <span class="ruby-identifier">options</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">extract_options!</span> <span class="ruby-identifier">name</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">versions</span> = <span class="ruby-identifier">args</span> <span class="ruby-comment"># Set the message to be shown in logs. Uses the git repo if one is given,</span> <span class="ruby-comment"># otherwise use name (version).</span> <span class="ruby-identifier">parts</span>, <span class="ruby-identifier">message</span> = [ <span class="ruby-identifier">quote</span>(<span class="ruby-identifier">name</span>) ], <span class="ruby-identifier">name</span>.<span class="ruby-identifier">dup</span> <span class="ruby-comment"># Output a comment above the gem declaration.</span> <span class="ruby-identifier">comment</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value">:comment</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">versions</span> = <span class="ruby-identifier">versions</span>.<span class="ruby-identifier">any?</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">versions</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value">:version</span>) <span class="ruby-identifier">_versions</span> = <span class="ruby-constant">Array</span>(<span class="ruby-identifier">versions</span>) <span class="ruby-identifier">_versions</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">version</span><span class="ruby-operator">|</span> <span class="ruby-identifier">parts</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">quote</span>(<span class="ruby-identifier">version</span>) <span class="ruby-keyword">end</span> <span class="ruby-identifier">message</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-node">&quot; (#{_versions.join(&quot;, &quot;)})&quot;</span> <span class="ruby-keyword">end</span> <span class="ruby-identifier">message</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:git</span>] <span class="ruby-keyword">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:git</span>] <span class="ruby-identifier">log</span> <span class="ruby-value">:gemfile</span>, <span class="ruby-identifier">message</span> <span class="ruby-identifier">parts</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">quote</span>(<span class="ruby-identifier">options</span>) <span class="ruby-keyword">unless</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-identifier">in_root</span> <span class="ruby-keyword">do</span> <span class="ruby-identifier">str</span> = [] <span class="ruby-keyword">if</span> <span class="ruby-identifier">comment</span> <span class="ruby-identifier">comment</span>.<span class="ruby-identifier">each_line</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">comment_line</span><span class="ruby-operator">|</span> <span class="ruby-identifier">str</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">indentation</span> <span class="ruby-identifier">str</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-node">&quot;# #{comment_line}&quot;</span> <span class="ruby-keyword">end</span> <span class="ruby-identifier">str</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-string">&quot;\n&quot;</span> <span class="ruby-keyword">end</span> <span class="ruby-identifier">str</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">indentation</span> <span class="ruby-identifier">str</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-node">&quot;gem #{parts.join(&quot;, &quot;)}&quot;</span> <span class="ruby-identifier">append_file_with_newline</span> <span class="ruby-string">&quot;Gemfile&quot;</span>, <span class="ruby-identifier">str</span>.<span class="ruby-identifier">join</span>, <span class="ruby-value">verbose:</span> <span class="ruby-keyword">false</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-gem_group"> <b>gem_group</b>(*names, &amp;block) <a href="../../../classes/Rails/Generators/Actions.html#method-i-gem_group" name="method-i-gem_group" class="permalink">Link</a> </h3> <div class="description"> <p>Wraps gem entries inside a group.</p> <pre><code>gem_group :development, :test do gem &quot;rspec-rails&quot; end </code></pre> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-gem_group_source')" id="l_method-i-gem_group_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/railties/lib/rails/generators/actions.rb#L111" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-gem_group_source" class="dyn-source"> <pre><span class="ruby-comment"># File railties/lib/rails/generators/actions.rb, line 111</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">gem_group</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">names</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-identifier">options</span> = <span class="ruby-identifier">names</span>.<span class="ruby-identifier">extract_options!</span> <span class="ruby-identifier">str</span> = <span class="ruby-identifier">names</span>.<span class="ruby-identifier">map</span>(<span class="ruby-operator">&amp;</span><span class="ruby-value">:inspect</span>) <span class="ruby-identifier">str</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">quote</span>(<span class="ruby-identifier">options</span>) <span class="ruby-keyword">unless</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-identifier">str</span> = <span class="ruby-identifier">str</span>.<span class="ruby-identifier">join</span>(<span class="ruby-string">&quot;, &quot;</span>) <span class="ruby-identifier">log</span> <span class="ruby-value">:gemfile</span>, <span class="ruby-node">&quot;group #{str}&quot;</span> <span class="ruby-identifier">in_root</span> <span class="ruby-keyword">do</span> <span class="ruby-identifier">append_file_with_newline</span> <span class="ruby-string">&quot;Gemfile&quot;</span>, <span class="ruby-node">&quot;\ngroup #{str} do&quot;</span>, <span class="ruby-value">force:</span> <span class="ruby-keyword">true</span> <span class="ruby-identifier">with_indentation</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-identifier">append_file_with_newline</span> <span class="ruby-string">&quot;Gemfile&quot;</span>, <span class="ruby-string">&quot;end&quot;</span>, <span class="ruby-value">force:</span> <span class="ruby-keyword">true</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-generate"> <b>generate</b>(what, *args) <a href="../../../classes/Rails/Generators/Actions.html#method-i-generate" name="method-i-generate" class="permalink">Link</a> </h3> <div class="description"> <p>Runs another generator.</p> <pre><code>generate &quot;scaffold&quot;, &quot;Post title:string body:text&quot; generate &quot;scaffold&quot;, &quot;Post&quot;, &quot;title:string&quot;, &quot;body:text&quot; </code></pre> <p>The first argument is the generator name, and the remaining arguments are joined together and passed to the generator.</p> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-generate_source')" id="l_method-i-generate_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/railties/lib/rails/generators/actions.rb#L332" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-generate_source" class="dyn-source"> <pre><span class="ruby-comment"># File railties/lib/rails/generators/actions.rb, line 332</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">generate</span>(<span class="ruby-identifier">what</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span>) <span class="ruby-identifier">log</span> <span class="ruby-value">:generate</span>, <span class="ruby-identifier">what</span> <span class="ruby-identifier">options</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">extract_options!</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:abort_on_failure</span>] = <span class="ruby-operator">!</span><span class="ruby-identifier">options</span>[<span class="ruby-value">:inline</span>] <span class="ruby-identifier">rails_command</span> <span class="ruby-node">&quot;generate #{what} #{args.join(&quot; &quot;)}&quot;</span>, <span class="ruby-identifier">options</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-git"> <b>git</b>(commands = {}) <a href="../../../classes/Rails/Generators/Actions.html#method-i-git" name="method-i-git" class="permalink">Link</a> </h3> <div class="description"> <p>Runs one or more git commands.</p> <pre><code>git :init # =&gt; runs `git init` git add: &quot;this.file that.rb&quot; # =&gt; runs `git add this.file that.rb` git commit: &quot;-m &#39;First commit&#39;&quot; # =&gt; runs `git commit -m &#39;First commit&#39;` git add: &quot;good.rb&quot;, rm: &quot;bad.cxx&quot; # =&gt; runs `git add good.rb; git rm bad.cxx` </code></pre> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-git_source')" id="l_method-i-git_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/railties/lib/rails/generators/actions.rb#L237" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-git_source" class="dyn-source"> <pre><span class="ruby-comment"># File railties/lib/rails/generators/actions.rb, line 237</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">git</span>(<span class="ruby-identifier">commands</span> = {}) <span class="ruby-keyword">if</span> <span class="ruby-identifier">commands</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Symbol</span>) <span class="ruby-identifier">run</span> <span class="ruby-node">&quot;git #{commands}&quot;</span> <span class="ruby-keyword">else</span> <span class="ruby-identifier">commands</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">cmd</span>, <span class="ruby-identifier">options</span><span class="ruby-operator">|</span> <span class="ruby-identifier">run</span> <span class="ruby-node">&quot;git #{cmd} #{options}&quot;</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-github"> <b>github</b>(repo, options = {}, &amp;block) <a href="../../../classes/Rails/Generators/Actions.html#method-i-github" name="method-i-github" class="permalink">Link</a> </h3> <div class="description"> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-github_source')" id="l_method-i-github_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/railties/lib/rails/generators/actions.rb#L125" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-github_source" class="dyn-source"> <pre><span class="ruby-comment"># File railties/lib/rails/generators/actions.rb, line 125</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">github</span>(<span class="ruby-identifier">repo</span>, <span class="ruby-identifier">options</span> = {}, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-identifier">str</span> = [<span class="ruby-identifier">quote</span>(<span class="ruby-identifier">repo</span>)] <span class="ruby-identifier">str</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">quote</span>(<span class="ruby-identifier">options</span>) <span class="ruby-keyword">unless</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-identifier">str</span> = <span class="ruby-identifier">str</span>.<span class="ruby-identifier">join</span>(<span class="ruby-string">&quot;, &quot;</span>) <span class="ruby-identifier">log</span> <span class="ruby-value">:github</span>, <span class="ruby-node">&quot;github #{str}&quot;</span> <span class="ruby-identifier">in_root</span> <span class="ruby-keyword">do</span> <span class="ruby-keyword">if</span> <span class="ruby-ivar">@indentation</span>.<span class="ruby-identifier">zero?</span> <span class="ruby-identifier">append_file_with_newline</span> <span class="ruby-string">&quot;Gemfile&quot;</span>, <span class="ruby-node">&quot;\ngithub #{str} do&quot;</span>, <span class="ruby-value">force:</span> <span class="ruby-keyword">true</span> <span class="ruby-keyword">else</span> <span class="ruby-identifier">append_file_with_newline</span> <span class="ruby-string">&quot;Gemfile&quot;</span>, <span class="ruby-node">&quot;#{indentation}github #{str} do&quot;</span>, <span class="ruby-value">force:</span> <span class="ruby-keyword">true</span> <span class="ruby-keyword">end</span> <span class="ruby-identifier">with_indentation</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-identifier">append_file_with_newline</span> <span class="ruby-string">&quot;Gemfile&quot;</span>, <span class="ruby-node">&quot;#{indentation}end&quot;</span>, <span class="ruby-value">force:</span> <span class="ruby-keyword">true</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-initializer"> <b>initializer</b>(filename, data = nil) <a href="../../../classes/Rails/Generators/Actions.html#method-i-initializer" name="method-i-initializer" class="permalink">Link</a> </h3> <div class="description"> <p>Creates an initializer file in <code>config/initializers/</code>. The code can be specified as an argument or as the return value of the block.</p> <pre><code>initializer &quot;api.rb&quot;, &lt;&lt;~RUBY API_KEY = &quot;123456&quot; RUBY initializer &quot;api.rb&quot; do %(API_KEY = &quot;123456&quot;) end </code></pre> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-initializer_source')" id="l_method-i-initializer_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/railties/lib/rails/generators/actions.rb#L319" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-initializer_source" class="dyn-source"> <pre><span class="ruby-comment"># File railties/lib/rails/generators/actions.rb, line 319</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">initializer</span>(<span class="ruby-identifier">filename</span>, <span class="ruby-identifier">data</span> = <span class="ruby-keyword">nil</span>) <span class="ruby-identifier">log</span> <span class="ruby-value">:initializer</span>, <span class="ruby-identifier">filename</span> <span class="ruby-identifier">data</span> <span class="ruby-operator">||=</span> <span class="ruby-keyword">yield</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span> <span class="ruby-identifier">create_file</span>(<span class="ruby-node">&quot;config/initializers/#{filename}&quot;</span>, <span class="ruby-identifier">optimize_indentation</span>(<span class="ruby-identifier">data</span>), <span class="ruby-value">verbose:</span> <span class="ruby-keyword">false</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-lib"> <b>lib</b>(filename, data = nil) <a href="../../../classes/Rails/Generators/Actions.html#method-i-lib" name="method-i-lib" class="permalink">Link</a> </h3> <div class="description"> <p>Creates a file in <code>lib/</code>. The contents can be specified as an argument or as the return value of the block.</p> <pre><code>lib &quot;foreign.rb&quot;, &lt;&lt;~RUBY # Foreign code is fun RUBY lib &quot;foreign.rb&quot; do &quot;# Foreign code is fun&quot; end </code></pre> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-lib_source')" id="l_method-i-lib_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/railties/lib/rails/generators/actions.rb#L275" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-lib_source" class="dyn-source"> <pre><span class="ruby-comment"># File railties/lib/rails/generators/actions.rb, line 275</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">lib</span>(<span class="ruby-identifier">filename</span>, <span class="ruby-identifier">data</span> = <span class="ruby-keyword">nil</span>) <span class="ruby-identifier">log</span> <span class="ruby-value">:lib</span>, <span class="ruby-identifier">filename</span> <span class="ruby-identifier">data</span> <span class="ruby-operator">||=</span> <span class="ruby-keyword">yield</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span> <span class="ruby-identifier">create_file</span>(<span class="ruby-node">&quot;lib/#{filename}&quot;</span>, <span class="ruby-identifier">optimize_indentation</span>(<span class="ruby-identifier">data</span>), <span class="ruby-value">verbose:</span> <span class="ruby-keyword">false</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-rails_command"> <b>rails_command</b>(command, options = {}) <a href="../../../classes/Rails/Generators/Actions.html#method-i-rails_command" name="method-i-rails_command" class="permalink">Link</a> </h3> <div class="description"> <p>Runs the specified Rails command.</p> <pre><code>rails_command &quot;db:migrate&quot; rails_command &quot;db:migrate&quot;, env: &quot;production&quot; rails_command &quot;db:migrate&quot;, abort_on_failure: true rails_command &quot;stats&quot;, capture: true rails_command &quot;gems:install&quot;, sudo: true </code></pre> <h4 id="method-i-rails_command-label-Options">Options</h4> <dl class="rdoc-list label-list"><dt><code>:env</code> <dd> <p>The Rails environment in which to run the command. Defaults to <code>ENV[&quot;RAILS_ENV&quot;] || &quot;development&quot;</code>.</p> </dd><dt><code>:abort_on_failure</code> <dd> <p>Whether to halt the generator if the command exits with a non-success exit status.</p> </dd><dt><code>:capture</code> <dd> <p>Whether to capture and return the output of the command.</p> </dd><dt><code>:sudo</code> <dd> <p>Whether to run the command using <code>sudo</code>.</p> </dd></dl> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-rails_command_source')" id="l_method-i-rails_command_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/railties/lib/rails/generators/actions.rb#L391" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-rails_command_source" class="dyn-source"> <pre><span class="ruby-comment"># File railties/lib/rails/generators/actions.rb, line 391</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">rails_command</span>(<span class="ruby-identifier">command</span>, <span class="ruby-identifier">options</span> = {}) <span class="ruby-keyword">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:inline</span>] <span class="ruby-identifier">log</span> <span class="ruby-value">:rails</span>, <span class="ruby-identifier">command</span> <span class="ruby-identifier">command</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span> = <span class="ruby-constant">Shellwords</span>.<span class="ruby-identifier">split</span>(<span class="ruby-identifier">command</span>) <span class="ruby-identifier">in_root</span> <span class="ruby-keyword">do</span> <span class="ruby-identifier">silence_warnings</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">::</span><span class="ruby-constant">Rails</span><span class="ruby-operator">::</span><span class="ruby-constant">Command</span>.<span class="ruby-identifier">invoke</span>(<span class="ruby-identifier">command</span>, <span class="ruby-identifier">args</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>) <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">else</span> <span class="ruby-identifier">execute_command</span> <span class="ruby-value">:rails</span>, <span class="ruby-identifier">command</span>, <span class="ruby-identifier">options</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-rake"> <b>rake</b>(command, options = {}) <a href="../../../classes/Rails/Generators/Actions.html#method-i-rake" name="method-i-rake" class="permalink">Link</a> </h3> <div class="description"> <p>Runs the specified Rake task.</p> <pre><code>rake &quot;db:migrate&quot; rake &quot;db:migrate&quot;, env: &quot;production&quot; rake &quot;db:migrate&quot;, abort_on_failure: true rake &quot;stats&quot;, capture: true rake &quot;gems:install&quot;, sudo: true </code></pre> <h4 id="method-i-rake-label-Options">Options</h4> <dl class="rdoc-list label-list"><dt><code>:env</code> <dd> <p>The Rails environment in which to run the task. Defaults to <code>ENV[&quot;RAILS_ENV&quot;] || &quot;development&quot;</code>.</p> </dd><dt><code>:abort_on_failure</code> <dd> <p>Whether to halt the generator if the task exits with a non-success exit status.</p> </dd><dt><code>:capture</code> <dd> <p>Whether to capture and return the output of the task.</p> </dd><dt><code>:sudo</code> <dd> <p>Whether to run the task using <code>sudo</code>.</p> </dd></dl> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-rake_source')" id="l_method-i-rake_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/railties/lib/rails/generators/actions.rb#L364" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-rake_source" class="dyn-source"> <pre><span class="ruby-comment"># File railties/lib/rails/generators/actions.rb, line 364</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">rake</span>(<span class="ruby-identifier">command</span>, <span class="ruby-identifier">options</span> = {}) <span class="ruby-identifier">execute_command</span> <span class="ruby-value">:rake</span>, <span class="ruby-identifier">command</span>, <span class="ruby-identifier">options</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-rakefile"> <b>rakefile</b>(filename, data = nil) <a href="../../../classes/Rails/Generators/Actions.html#method-i-rakefile" name="method-i-rakefile" class="permalink">Link</a> </h3> <div class="description"> <p>Creates a Rake tasks file in <code>lib/tasks/</code>. The code can be specified as an argument or as the return value of the block.</p> <pre><code>rakefile &quot;bootstrap.rake&quot;, &lt;&lt;~RUBY task :bootstrap do puts &quot;Boots! Boots! Boots!&quot; end RUBY rakefile &quot;bootstrap.rake&quot; do project = ask(&quot;What is the UNIX name of your project?&quot;) &lt;&lt;~RUBY namespace :#{project} do task :bootstrap do puts &quot;Boots! Boots! Boots!&quot; end end RUBY end </code></pre> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-rakefile_source')" id="l_method-i-rakefile_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/railties/lib/rails/generators/actions.rb#L302" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-rakefile_source" class="dyn-source"> <pre><span class="ruby-comment"># File railties/lib/rails/generators/actions.rb, line 302</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">rakefile</span>(<span class="ruby-identifier">filename</span>, <span class="ruby-identifier">data</span> = <span class="ruby-keyword">nil</span>) <span class="ruby-identifier">log</span> <span class="ruby-value">:rakefile</span>, <span class="ruby-identifier">filename</span> <span class="ruby-identifier">data</span> <span class="ruby-operator">||=</span> <span class="ruby-keyword">yield</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span> <span class="ruby-identifier">create_file</span>(<span class="ruby-node">&quot;lib/tasks/#{filename}&quot;</span>, <span class="ruby-identifier">optimize_indentation</span>(<span class="ruby-identifier">data</span>), <span class="ruby-value">verbose:</span> <span class="ruby-keyword">false</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-readme"> <b>readme</b>(path) <a href="../../../classes/Rails/Generators/Actions.html#method-i-readme" name="method-i-readme" class="permalink">Link</a> </h3> <div class="description"> <p>Reads the given file at the source root and prints it in the console.</p> <pre><code>readme &quot;README&quot; </code></pre> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-readme_source')" id="l_method-i-readme_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/railties/lib/rails/generators/actions.rb#L442" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-readme_source" class="dyn-source"> <pre><span class="ruby-comment"># File railties/lib/rails/generators/actions.rb, line 442</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">readme</span>(<span class="ruby-identifier">path</span>) <span class="ruby-identifier">log</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">read</span>(<span class="ruby-identifier">find_in_source_paths</span>(<span class="ruby-identifier">path</span>)) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-route"> <b>route</b>(routing_code, namespace: nil) <a href="../../../classes/Rails/Generators/Actions.html#method-i-route" name="method-i-route" class="permalink">Link</a> </h3> <div class="description"> <p>Make an entry in Rails routing file <code>config/routes.rb</code></p> <pre><code>route &quot;root &#39;welcome#index&#39;&quot; route &quot;root &#39;admin#index&#39;&quot;, namespace: :admin </code></pre> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-route_source')" id="l_method-i-route_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/railties/lib/rails/generators/actions.rb#L409" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-route_source" class="dyn-source"> <pre><span class="ruby-comment"># File railties/lib/rails/generators/actions.rb, line 409</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">route</span>(<span class="ruby-identifier">routing_code</span>, <span class="ruby-value">namespace:</span> <span class="ruby-keyword">nil</span>) <span class="ruby-identifier">namespace</span> = <span class="ruby-constant">Array</span>(<span class="ruby-identifier">namespace</span>) <span class="ruby-identifier">namespace_pattern</span> = <span class="ruby-identifier">route_namespace_pattern</span>(<span class="ruby-identifier">namespace</span>) <span class="ruby-identifier">routing_code</span> = <span class="ruby-identifier">namespace</span>.<span class="ruby-identifier">reverse</span>.<span class="ruby-identifier">reduce</span>(<span class="ruby-identifier">routing_code</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">code</span>, <span class="ruby-identifier">name</span><span class="ruby-operator">|</span> <span class="ruby-node">&quot;namespace :#{name} do\n#{rebase_indentation(code, 2)}end&quot;</span> <span class="ruby-keyword">end</span> <span class="ruby-identifier">log</span> <span class="ruby-value">:route</span>, <span class="ruby-identifier">routing_code</span> <span class="ruby-identifier">in_root</span> <span class="ruby-keyword">do</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">namespace_match</span> = <span class="ruby-identifier">match_file</span>(<span class="ruby-string">&quot;config/routes.rb&quot;</span>, <span class="ruby-identifier">namespace_pattern</span>) <span class="ruby-identifier">base_indent</span>, <span class="ruby-operator">*</span>, <span class="ruby-identifier">existing_block_indent</span> = <span class="ruby-identifier">namespace_match</span>.<span class="ruby-identifier">captures</span>.<span class="ruby-identifier">compact</span>.<span class="ruby-identifier">map</span>(<span class="ruby-operator">&amp;</span><span class="ruby-value">:length</span>) <span class="ruby-identifier">existing_line_pattern</span> = <span class="ruby-regexp">/^[ ]{,#{existing_block_indent}}\S.+\n?/</span> <span class="ruby-identifier">routing_code</span> = <span class="ruby-identifier">rebase_indentation</span>(<span class="ruby-identifier">routing_code</span>, <span class="ruby-identifier">base_indent</span> <span class="ruby-operator">+</span> <span class="ruby-value">2</span>).<span class="ruby-identifier">gsub</span>(<span class="ruby-identifier">existing_line_pattern</span>, <span class="ruby-string">&quot;&quot;</span>) <span class="ruby-identifier">namespace_pattern</span> = <span class="ruby-regexp">/#{Regexp.escape namespace_match.to_s}/</span> <span class="ruby-keyword">end</span> <span class="ruby-identifier">inject_into_file</span> <span class="ruby-string">&quot;config/routes.rb&quot;</span>, <span class="ruby-identifier">routing_code</span>, <span class="ruby-value">after:</span> <span class="ruby-identifier">namespace_pattern</span>, <span class="ruby-value">verbose:</span> <span class="ruby-keyword">false</span>, <span class="ruby-value">force:</span> <span class="ruby-keyword">false</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">behavior</span> <span class="ruby-operator">==</span> <span class="ruby-value">:revoke</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">namespace</span>.<span class="ruby-identifier">any?</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">namespace_match</span> <span class="ruby-identifier">empty_block_pattern</span> = <span class="ruby-regexp">/(#{namespace_pattern})((?:\s*end\n){1,#{namespace.size}})/</span> <span class="ruby-identifier">gsub_file</span> <span class="ruby-string">&quot;config/routes.rb&quot;</span>, <span class="ruby-identifier">empty_block_pattern</span>, <span class="ruby-value">verbose:</span> <span class="ruby-keyword">false</span>, <span class="ruby-value">force:</span> <span class="ruby-keyword">true</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">matched</span><span class="ruby-operator">|</span> <span class="ruby-identifier">beginning</span>, <span class="ruby-identifier">ending</span> = <span class="ruby-identifier">empty_block_pattern</span>.<span class="ruby-identifier">match</span>(<span class="ruby-identifier">matched</span>).<span class="ruby-identifier">captures</span> <span class="ruby-identifier">ending</span>.<span class="ruby-identifier">sub!</span>(<span class="ruby-regexp">/\A\s*end\n/</span>, <span class="ruby-string">&quot;&quot;</span>) <span class="ruby-keyword">while</span> <span class="ruby-operator">!</span><span class="ruby-identifier">ending</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">beginning</span>.<span class="ruby-identifier">sub!</span>(<span class="ruby-regexp">/^[ ]*namespace .+ do\n\s*\z/</span>, <span class="ruby-string">&quot;&quot;</span>) <span class="ruby-identifier">beginning</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">ending</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-vendor"> <b>vendor</b>(filename, data = nil) <a href="../../../classes/Rails/Generators/Actions.html#method-i-vendor" name="method-i-vendor" class="permalink">Link</a> </h3> <div class="description"> <p>Creates a file in <code>vendor/</code>. The contents can be specified as an argument or as the return value of the block.</p> <pre><code>vendor &quot;foreign.rb&quot;, &lt;&lt;~RUBY # Foreign code is fun RUBY vendor &quot;foreign.rb&quot; do &quot;# Foreign code is fun&quot; end </code></pre> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-vendor_source')" id="l_method-i-vendor_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/railties/lib/rails/generators/actions.rb#L258" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-vendor_source" class="dyn-source"> <pre><span class="ruby-comment"># File railties/lib/rails/generators/actions.rb, line 258</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">vendor</span>(<span class="ruby-identifier">filename</span>, <span class="ruby-identifier">data</span> = <span class="ruby-keyword">nil</span>) <span class="ruby-identifier">log</span> <span class="ruby-value">:vendor</span>, <span class="ruby-identifier">filename</span> <span class="ruby-identifier">data</span> <span class="ruby-operator">||=</span> <span class="ruby-keyword">yield</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span> <span class="ruby-identifier">create_file</span>(<span class="ruby-node">&quot;vendor/#{filename}&quot;</span>, <span class="ruby-identifier">optimize_indentation</span>(<span class="ruby-identifier">data</span>), <span class="ruby-value">verbose:</span> <span class="ruby-keyword">false</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <h2 class="sectiontitle">Instance Private methods</h2> <div class="method"> <h3 class="title method-title" id="method-i-execute_command"> <b>execute_command</b>(executor, command, options = {}) <a href="../../../classes/Rails/Generators/Actions.html#method-i-execute_command" name="method-i-execute_command" class="permalink">Link</a> </h3> <div class="description"> <p>Runs the supplied command using either “rake …” or “rails …” based on the executor parameter provided.</p> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-execute_command_source')" id="l_method-i-execute_command_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/railties/lib/rails/generators/actions.rb#L461" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-execute_command_source" class="dyn-source"> <pre><span class="ruby-comment"># File railties/lib/rails/generators/actions.rb, line 461</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">execute_command</span>(<span class="ruby-identifier">executor</span>, <span class="ruby-identifier">command</span>, <span class="ruby-identifier">options</span> = {}) <span class="ruby-comment"># :doc:</span> <span class="ruby-identifier">log</span> <span class="ruby-identifier">executor</span>, <span class="ruby-identifier">command</span> <span class="ruby-identifier">sudo</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:sudo</span>] <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-operator">!</span><span class="ruby-constant">Gem</span>.<span class="ruby-identifier">win_platform?</span> <span class="ruby-operator">?</span> <span class="ruby-string">&quot;sudo &quot;</span> <span class="ruby-operator">:</span> <span class="ruby-string">&quot;&quot;</span> <span class="ruby-identifier">config</span> = { <span class="ruby-value">env:</span> { <span class="ruby-string">&quot;RAILS_ENV&quot;</span> <span class="ruby-operator">=&gt;</span> (<span class="ruby-identifier">options</span>[<span class="ruby-value">:env</span>] <span class="ruby-operator">||</span> <span class="ruby-constant">ENV</span>[<span class="ruby-string">&quot;RAILS_ENV&quot;</span>] <span class="ruby-operator">||</span> <span class="ruby-string">&quot;development&quot;</span>) }, <span class="ruby-value">verbose:</span> <span class="ruby-keyword">false</span>, <span class="ruby-value">capture:</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:capture</span>], <span class="ruby-value">abort_on_failure:</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:abort_on_failure</span>], } <span class="ruby-identifier">in_root</span> { <span class="ruby-identifier">run</span>(<span class="ruby-node">&quot;#{sudo}#{Shellwords.escape Gem.ruby} bin/#{executor} #{command}&quot;</span>, <span class="ruby-identifier">config</span>) } <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-indentation"> <b>indentation</b>() <a href="../../../classes/Rails/Generators/Actions.html#method-i-indentation" name="method-i-indentation" class="permalink">Link</a> </h3> <div class="description"> <p>Indent the <code>Gemfile</code> to the depth of @indentation</p> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-indentation_source')" id="l_method-i-indentation_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/railties/lib/rails/generators/actions.rb#L494" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-indentation_source" class="dyn-source"> <pre><span class="ruby-comment"># File railties/lib/rails/generators/actions.rb, line 494</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">indentation</span> <span class="ruby-comment"># :doc:</span> <span class="ruby-string">&quot; &quot;</span> <span class="ruby-operator">*</span> <span class="ruby-ivar">@indentation</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-log"> <b>log</b>(*args) <a href="../../../classes/Rails/Generators/Actions.html#method-i-log" name="method-i-log" class="permalink">Link</a> </h3> <div class="description"> <p>Define log for backwards compatibility. If just one argument is sent, invoke say, otherwise invoke say_status. Differently from say and similarly to say_status, this method respects the quiet? option given.</p> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-log_source')" id="l_method-i-log_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/railties/lib/rails/generators/actions.rb#L450" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-log_source" class="dyn-source"> <pre><span class="ruby-comment"># File railties/lib/rails/generators/actions.rb, line 450</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">log</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>) <span class="ruby-comment"># :doc:</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">==</span> <span class="ruby-value">1</span> <span class="ruby-identifier">say</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">first</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">quiet?</span> <span class="ruby-keyword">else</span> <span class="ruby-identifier">args</span> <span class="ruby-operator">&lt;&lt;</span> (<span class="ruby-identifier">behavior</span> <span class="ruby-operator">==</span> <span class="ruby-value">:invoke</span> <span class="ruby-operator">?</span> <span class="ruby-value">:green</span> <span class="ruby-operator">:</span> <span class="ruby-value">:red</span>) <span class="ruby-identifier">say_status</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>) <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-optimize_indentation"> <b>optimize_indentation</b>(value, amount = 0) <a href="../../../classes/Rails/Generators/Actions.html#method-i-optimize_indentation" name="method-i-optimize_indentation" class="permalink">Link</a> </h3> <div class="description"> <p>Returns optimized string with indentation</p> </div> <div class="aka"> Also aliased as: <a href="Actions.html#method-i-rebase_indentation">rebase_indentation</a> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-optimize_indentation_source')" id="l_method-i-optimize_indentation_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/railties/lib/rails/generators/actions.rb#L487" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-optimize_indentation_source" class="dyn-source"> <pre><span class="ruby-comment"># File railties/lib/rails/generators/actions.rb, line 487</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">optimize_indentation</span>(<span class="ruby-identifier">value</span>, <span class="ruby-identifier">amount</span> = <span class="ruby-value">0</span>) <span class="ruby-comment"># :doc:</span> <span class="ruby-keyword">return</span> <span class="ruby-node">&quot;#{value}\n&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">String</span>) <span class="ruby-node">&quot;#{value.strip_heredoc.indent(amount).chomp}\n&quot;</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-quote"> <b>quote</b>(value) <a href="../../../classes/Rails/Generators/Actions.html#method-i-quote" name="method-i-quote" class="permalink">Link</a> </h3> <div class="description"> <p>Always returns value in double quotes.</p> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-quote_source')" id="l_method-i-quote_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/railties/lib/rails/generators/actions.rb#L475" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-quote_source" class="dyn-source"> <pre><span class="ruby-comment"># File railties/lib/rails/generators/actions.rb, line 475</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">quote</span>(<span class="ruby-identifier">value</span>) <span class="ruby-comment"># :doc:</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:each_pair</span> <span class="ruby-keyword">return</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">map</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">k</span>, <span class="ruby-identifier">v</span><span class="ruby-operator">|</span> <span class="ruby-node">&quot;#{k}: #{quote(v)}&quot;</span> <span class="ruby-keyword">end</span>.<span class="ruby-identifier">join</span>(<span class="ruby-string">&quot;, &quot;</span>) <span class="ruby-keyword">end</span> <span class="ruby-keyword">return</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">inspect</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">is_a?</span> <span class="ruby-constant">String</span> <span class="ruby-node">&quot;\&quot;#{value.tr(&quot;&#39;&quot;, &#39;&quot;&#39;)}\&quot;&quot;</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-with_indentation"> <b>with_indentation</b>(&amp;block) <a href="../../../classes/Rails/Generators/Actions.html#method-i-with_indentation" name="method-i-with_indentation" class="permalink">Link</a> </h3> <div class="description"> <p>Manage <code>Gemfile</code> indentation for a DSL action block</p> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-with_indentation_source')" id="l_method-i-with_indentation_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/railties/lib/rails/generators/actions.rb#L499" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-with_indentation_source" class="dyn-source"> <pre><span class="ruby-comment"># File railties/lib/rails/generators/actions.rb, line 499</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">with_indentation</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-comment"># :doc:</span> <span class="ruby-ivar">@indentation</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span> <span class="ruby-identifier">instance_eval</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-keyword">ensure</span> <span class="ruby-ivar">@indentation</span> <span class="ruby-operator">-=</span> <span class="ruby-value">1</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> </div> </main> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10