app2engine 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -15,8 +15,8 @@ This tool intend to do most of them for you.
15
15
  ## Synopsis
16
16
 
17
17
  Create an new app to convert it to an Engine (avoid using underscores):
18
-
19
- rails new myengine #
18
+
19
+ rails new myengine
20
20
 
21
21
  Run `app2engine` in the root directory of this app you want to make an Engine:
22
22
 
@@ -28,7 +28,7 @@ Then convert it with Rake:
28
28
 
29
29
  Follow the instructions: To the main app's Gemfile, add
30
30
 
31
- gem 'myengine', :path => 'path/to/myengine'
31
+ gem 'myengine', :path => 'path/to/myengine'
32
32
 
33
33
  Use extras if you want:
34
34
 
@@ -50,6 +50,74 @@ In your main app's dir:
50
50
 
51
51
  (You can also verify routing is fine with `rake routes`)
52
52
 
53
+ ## Notes
54
+
55
+ ### Namespace them all !
56
+
57
+ The folders `{controllers,models,views}/myengine` are created because you should namespace your engine.
58
+
59
+ ### Production
60
+
61
+ In Production environment, you must comment (or set to `true`) the line in mainapp/config/environments/production.rb
62
+
63
+ # Disable Rails's static asset server
64
+ # In production, Apache or nginx will already do this
65
+ config.serve_static_assets = false
66
+
67
+ Because the engine modify the static assets by appending its own public folder. (A workaround may be to config you server)
68
+
69
+ ### Code reloading / overwriting / load order
70
+
71
+ This is a discussion about what happen if you want to modify behavior in the mainapp.
72
+
73
+ #### Models
74
+
75
+ Models are behaving especially bad with Engines.
76
+
77
+ In development, you need to load yourself the model, or only the model of the *engine* will be loaded.
78
+ To keep the auto-reloading, I only found this workaround (in the *engine*/app/blog/posts.rb):
79
+
80
+ if File.exist?(app = Rails.root.join("app/models/blog/posts.rb").to_s)
81
+ load app
82
+ end
83
+
84
+ This is counter-intuitive as you would feel that the mainapp should load the engine one if redefined.
85
+
86
+ In production, however, not using this trick works, but letting it will not hurt.
87
+
88
+ #### Controllers
89
+
90
+ Controllers of the mainapp overwrite the one of the engine, completely.
91
+ (The one of the engine will not be loaded if there is the same in the mainapp)
92
+
93
+ You then have to load the engine controller yourself, adding this at the top of the mainapp controller:
94
+
95
+ load "myengine/app/controllers/test_controller.rb"
96
+
97
+ There is the problem we do not know how to access that file,
98
+ because loading `app/controllers/test_controller.rb` would require the mainapp controller (again).
99
+
100
+ So again a workaround, I created a symlink in the mainapp root (called myengine) to the engine root.
101
+
102
+ In production, you also need to load the file itself (but you could do a simple `require` as it will loaded once, so no need to change anything).
103
+
104
+ #### Views
105
+
106
+ Views in the mainapp overwrite the views corresponding in the engine.
107
+
108
+ This is expected behavior, as you can mainly think that the paths of the engine are appended to the ones of the mainapp.
109
+
110
+ #### public/ folder, static assets
111
+
112
+ In development, public assets are served as expected, with the mainapp overwriting the engine.
113
+
114
+ In production, however, the engine files do not seem to be served at all.
115
+ (and if you do not do the trick said upper with serve\_static\_assets, even the mainapp files will not load)
116
+
117
+ Update:
118
+ It seems it works with changing syntax in the "static assets" initializer of the engine. The gem is then updated.
119
+ (delete your lib/myengine/engine.rb, and call again `rake engine` and `rake engine:extra` if you want)
120
+
53
121
  ## Author
54
122
 
55
123
  Benoit Daloze
@@ -7,12 +7,12 @@ module App2Engine
7
7
  gemfile = File.join(dir, 'Gemfile') and File.exist?(gemfile)
8
8
  raise "#{dir} is not a rails app"
9
9
  end
10
-
10
+
11
11
  tasks = App2Engine::Rake::Tasks.new
12
-
12
+
13
13
  puts "Appending the App2Engine tasks to the Rakefile"
14
14
  tasks.append_to_file(rakefile, "require 'app2engine/rake/tasks'\nApp2Engine::Rake::Tasks.new")
15
-
15
+
16
16
  puts "Adding app2engine in the Gemfile (necessary for Rake tasks to run)"
17
17
  tasks.append_to_file(gemfile, "gem 'app2engine'")
18
18
  end
@@ -4,7 +4,8 @@ require "rails"
4
4
  module __PROJECT__
5
5
  class Engine < Rails::Engine
6
6
  initializer "static assets" do |app|
7
- app.middleware.use ActionDispatch::Static, "#{root}/public"
7
+ # app.middleware.use ActionDispatch::Static, "#{root}/public" # Old way, does not work in production
8
+ app.middleware.insert_after ActionDispatch::Static, ActionDispatch::Static, "#{root}/public"
8
9
  end
9
10
  end
10
11
  end
@@ -6,7 +6,7 @@ class MigrationsGenerator < Rails::Generators::Base
6
6
  def self.source_root
7
7
  @source_root ||= File.expand_path('../../templates', __FILE__)
8
8
  end
9
-
9
+
10
10
  # Implement the required interface for Rails::Generators::Migration.
11
11
  def self.next_migration_number(dirname)
12
12
  if ActiveRecord::Base.timestamped_migrations
@@ -15,7 +15,7 @@ class MigrationsGenerator < Rails::Generators::Base
15
15
  "%.3d" % (current_migration_number(dirname) + 1)
16
16
  end
17
17
  end
18
-
18
+
19
19
  def create_migration_file
20
20
  # migration_template 'create_posts.rb', 'db/migrate/create_blog_posts.rb'
21
21
  # migration_template '__DIR___create_posts.rb', 'db/migrate/__DIR___create_posts.rb'
@@ -14,37 +14,37 @@ module App2Engine
14
14
  tasks.each { |t| send(t) }
15
15
  end
16
16
  end
17
-
17
+
18
18
  def gemspec
19
19
  define_task(:gemspec, "add Jeweler to the Rakefile to allow to build a gem which can be referenced from the main app") do
20
20
  add_file('__project__.gemspec')
21
21
  end
22
22
  end
23
-
23
+
24
24
  def routes
25
25
  define_task(:routes, "Change routes to not have a reference to the application, but to the main app") do
26
26
  replace_line('config/routes.rb', "#{@project}::Application.routes.draw do", "Rails.application.routes.draw do")
27
27
  end
28
28
  end
29
-
29
+
30
30
  def hierarchy
31
31
  define_task(:hierarchy, "add the basic hierarchy for the Engine") do
32
32
  add_file('lib/__project__.rb')
33
33
  mkdir("lib/#{@dir}")
34
34
  add_file('lib/__project__/engine.rb')
35
-
35
+
36
36
  mkdir("app/controllers/__project__")
37
37
  mkdir("app/models/__project__")
38
38
  mkdir("app/views/__project__")
39
39
  end
40
40
  end
41
-
41
+
42
42
  def initializers
43
43
  define_task(:initializers, "remove initializers as they would conflict and create NameError") do
44
44
  move_dir('config/initializers', 'config/org_initializers')
45
45
  end
46
46
  end
47
-
47
+
48
48
  def generators
49
49
  define_task(:generators, "add the basic code for generators (needed for migrations)") do
50
50
  mkdir('lib/generators/__project__/migrations/templates')
@@ -9,7 +9,7 @@ module App2Engine
9
9
  Sass::Plugin.add_template_location(template_location, css_location)
10
10
  end
11
11
  SASS
12
-
12
+
13
13
  def extra_tasks
14
14
  tasks = %w[sass]
15
15
  task :extra => tasks.map { |t| "engine:extra:" << t }
@@ -17,7 +17,7 @@ SASS
17
17
  tasks.each { |t| send(t) }
18
18
  end
19
19
  end
20
-
20
+
21
21
  def sass
22
22
  define_task(:sass, "configure the project to be used with Sass") do
23
23
  append_in_class('lib/__project__/engine.rb', SASS)
@@ -7,7 +7,7 @@ require 'app2engine/rake/extra_tasks'
7
7
 
8
8
  require 'term/ansicolor'
9
9
  class String
10
- [:green, :red, :black, :blue].each { |method|
10
+ [:green, :red, :black, :blue, :yellow, :white].each { |method|
11
11
  define_method(method) {
12
12
  Term::ANSIColor.send(method, self)
13
13
  }
@@ -22,7 +22,7 @@ module App2Engine
22
22
  def initialize
23
23
  @dir = File.basename(File.expand_path("."))
24
24
  @project = @dir.split(/[^A-Za-z0-9]/).map(&:capitalize).join # Camelize
25
-
25
+
26
26
  namespace :engine do
27
27
  convert_tasks
28
28
  extra_tasks
@@ -38,28 +38,28 @@ module App2Engine
38
38
  block.call
39
39
  }
40
40
  end
41
-
41
+
42
42
  # Templates conventions
43
43
  def resolve_contents(contents)
44
44
  contents.gsub("__PROJECT__", @project).gsub("__DIR__", @dir)
45
45
  end
46
-
46
+
47
47
  def resolve_name(name)
48
48
  name.gsub("__project__", @dir)
49
49
  end
50
-
50
+
51
51
  def status status
52
52
  puts " #{status}"
53
53
  end
54
-
54
+
55
55
  def already_done what
56
- status "already done (#{what})".black
56
+ status "already done (#{what})".black # yellow
57
57
  end
58
-
58
+
59
59
  def file_contents file
60
60
  resolve_contents File.read(File.join(FILES_PATH, file))
61
61
  end
62
-
62
+
63
63
  def mkdir dir
64
64
  dir = resolve_name(dir)
65
65
  if File.directory? dir
@@ -69,7 +69,7 @@ module App2Engine
69
69
  status "Create #{dir}/".green
70
70
  end
71
71
  end
72
-
72
+
73
73
  def move_dir dir, to
74
74
  if File.directory? to
75
75
  already_done dir
@@ -78,7 +78,7 @@ module App2Engine
78
78
  status "Move #{dir} to #{to}".green
79
79
  end
80
80
  end
81
-
81
+
82
82
  def add_file file
83
83
  contents = file_contents(file)
84
84
  file = resolve_name(file)
@@ -89,7 +89,7 @@ module App2Engine
89
89
  status "Create #{file}".green
90
90
  end
91
91
  end
92
-
92
+
93
93
  def append_to_file file, contents
94
94
  file = resolve_name(file)
95
95
  if File.read(file).include?(contents)
@@ -102,7 +102,7 @@ module App2Engine
102
102
  status "Append #{file}".green
103
103
  end
104
104
  end
105
-
105
+
106
106
  def append_in_class(file, what)
107
107
  file = resolve_name(file)
108
108
  what = resolve_contents(what)
@@ -118,7 +118,7 @@ module App2Engine
118
118
  status "Append #{file}".green
119
119
  end
120
120
  end
121
-
121
+
122
122
  def replace_line(file, line, by)
123
123
  line = line.chomp + "\n"
124
124
  by = by.chomp + "\n"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: app2engine
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - eregon
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-04 00:00:00 +02:00
18
+ date: 2010-08-05 00:00:00 +02:00
19
19
  default_executable: app2engine
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency