Class: SectionsGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/sections_generator.rb

Overview

Generates the file structure for the plugin in Rails.

Instance Method Summary collapse

Instance Method Details

#create_sample_sectionObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/generators/sections_generator.rb', line 54

def create_sample_section
  say ''
  say 'STEP 3: Creating a sample section.', Thor::Shell::Color::BOLD
  say ''
  
  # Ask the user.
  return unless ['', 'y', 'yes'].include? ask('  Do you want to create a sample section? [Yn]').downcase
  say ''

  # Create the sample section directory.
  empty_directory "app/sections/hello_world"

  # Create the partial.
  create_file "app/sections/hello_world/_hello_world.html.erb", <<-END_STR
<%# This file contains the HTML for the 'hello world' section. %>

<div class="hello_world">
<h2>Hello World!</h2>
This is content defined by the hello world section.<br>
It is styled by CSS from the section. <br>
<br>
Click on this section.<br>
The behavior comes from the section's JavaScript file.<br>
</div>
  END_STR

  # Create the CSS file.
  create_file "app/sections/hello_world/hello_world.css", <<-END_STR
/* 
 * This file contains the CSS for the 'hello world' section. 
 *
 * Please note that all CSS in here should be relative to the container of the section,
 * in this case the ".hello" class.
 */

.hello_world { width: 350px; padding: 0 2ex 2ex; border: 2px dotted red; background-color: yellow; }
.hello_world .h2 { font-size: 1em; margin: 0 0 1ex; padding: 0; }
  END_STR
  
  # Create the JS file.
  create_file "app/sections/hello_world/hello_world.js", <<-END_STR
/*
 * This file contains the JavaScript for the 'hello world' section.
 *
 * Anything in here should be restrained to within the container of the section,
 * i.e. the <div> with the class 'hello_world'.
 */

$(function() {
$('.hello_world').click(function() {
  alert('The Hello World section says hello to the world!');
});
});
  END_STR

  say ''
  say '  I have created a sample section in '
  say 'app/sections/hello_world', Thor::Shell::Color::BOLD
  say '  To use it, simply put '
  say '<%= section :hello_world %>', Thor::Shell::Color::BOLD
  say '  into a view file.'
  say ''
  say '  Happy coding! :)'
  say ''
end

#create_section_assetsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/generators/sections_generator.rb', line 19

def create_section_assets
  say ''
  say 'STEP 2: Creating asset manifests.', Thor::Shell::Color::BOLD
  say ''
  create_file 'app/assets/javascripts/application_sections.js', <<-END_STR
// THIS FILE IS AUTOMATICALLY CREATED BY THE SECTIONS PLUGIN
// AND MUST BE LOADED BY YOUR PAGE.
// PLEASE DO NOT MODIFY IT MANUALLY.
//
  END_STR

  create_file 'app/assets/stylesheets/application_sections.css', <<-END_STR
/*
 * THIS FILE IS AUTOMATICALLY CREATED BY THE SECTIONS PLUGIN.
 * AND MUST BE LOADED BY YOUR PAGE.
 * PLEASE DO NOT MODIFY MANUALLY.
 */
  END_STR

  say ''
  say '  I have created two files to reference the assets from the sections.'
  say '  * '
  say 'app/assets/javascripts/application_sections.js', Thor::Shell::Color::BOLD
  say '  * '
  say 'app/assets/stylesheets/application_sections.css', Thor::Shell::Color::BOLD
  say ''
  say '  They should be checked into your code repository the way they are, and remain that way.'
  say '  When running "rake assets:precompile" during deployment,'
  say '  they will be updated with the actually used assets for proper operation in production mode.'
  say ''
  say '  You must make sure they are loaded into the asset pipeline, for example by'
  say '  requiring them in application.js and application.css.'
  say ''
end

#create_sections_folderObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/generators/sections_generator.rb', line 4

def create_sections_folder
  say ''
  say 'STEP 1: Creating sections folder', Thor::Shell::Color::BOLD
  say ''
  empty_directory "app/sections"
  inject_into_file 'config/application.rb',
                   "    config.assets.paths << 'app/sections'\n",
                   :after => /config.assets.enabled\s*=.*\n/
  say ''
  say '  I have made some modifications to '
  say 'config/application.rb', Thor::Shell::Color::BOLD
  say '  Please review them before submitting these changes to your code repository.'
  say ''
end