ember-rails-lite 0.12.0 → 0.18.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +220 -47
  3. data/lib/{ember-rails.rb → ember-rails-lite.rb} +0 -0
  4. data/lib/ember/rails/engine.rb +9 -4
  5. data/lib/ember/rails/version.rb +1 -1
  6. data/lib/ember_rails.rb +39 -28
  7. data/lib/generators/ember/adapter_generator.rb +21 -0
  8. data/lib/generators/ember/bootstrap_generator.rb +27 -9
  9. data/lib/generators/ember/component_generator.rb +28 -0
  10. data/lib/generators/ember/controller_generator.rb +1 -0
  11. data/lib/generators/ember/generator_helpers.rb +32 -5
  12. data/lib/generators/ember/install_generator.rb +169 -13
  13. data/lib/generators/ember/model_generator.rb +1 -0
  14. data/lib/generators/ember/resource_generator.rb +1 -0
  15. data/lib/generators/ember/resource_override.rb +8 -2
  16. data/lib/generators/ember/route_generator.rb +1 -0
  17. data/lib/generators/ember/template_generator.rb +4 -1
  18. data/lib/generators/ember/view_generator.rb +1 -0
  19. data/lib/generators/templates/adapter.js +5 -0
  20. data/lib/generators/templates/adapter.js.coffee +5 -0
  21. data/lib/generators/templates/adapter.js.em +3 -0
  22. data/lib/generators/templates/app.js +4 -2
  23. data/lib/generators/templates/app.js.coffee +3 -1
  24. data/lib/generators/templates/app.js.em +11 -0
  25. data/lib/generators/templates/{application.handlebars → application.hbs} +0 -0
  26. data/lib/generators/templates/application.js +4 -2
  27. data/lib/generators/templates/application.js.coffee +3 -1
  28. data/lib/generators/templates/application.js.em +10 -0
  29. data/lib/generators/templates/application_adapter.js +5 -0
  30. data/lib/generators/templates/application_adapter.js.coffee +5 -0
  31. data/lib/generators/templates/application_adapter.js.em +3 -0
  32. data/lib/generators/templates/array_controller.js +1 -1
  33. data/lib/generators/templates/array_controller.js.em +3 -0
  34. data/lib/generators/templates/component.js +4 -0
  35. data/lib/generators/templates/component.js.coffee +6 -0
  36. data/lib/generators/templates/component.js.em +6 -0
  37. data/lib/generators/templates/component.template.hbs +2 -0
  38. data/lib/generators/templates/controller.js.em +3 -0
  39. data/lib/generators/templates/model.js +1 -1
  40. data/lib/generators/templates/model.js.coffee +1 -1
  41. data/lib/generators/templates/model.js.em +12 -0
  42. data/lib/generators/templates/object_controller.js.em +3 -0
  43. data/lib/generators/templates/route.js.em +3 -0
  44. data/lib/generators/templates/router.js.em +5 -0
  45. data/lib/generators/templates/{template.handlebars → template.hbs} +0 -0
  46. data/lib/generators/templates/view.js.em +4 -0
  47. metadata +112 -92
  48. data/lib/ember/handlebars/template.rb +0 -102
  49. data/lib/ember/handlebars/version.rb +0 -5
  50. data/lib/generators/templates/store.js +0 -6
  51. data/lib/generators/templates/store.js.coffee +0 -6
@@ -0,0 +1,21 @@
1
+ require 'generators/ember/generator_helpers'
2
+
3
+ module Ember
4
+ module Generators
5
+ class AdapterGenerator < ::Rails::Generators::NamedBase
6
+ include Ember::Generators::GeneratorHelpers
7
+
8
+ source_root File.expand_path("../../templates", __FILE__)
9
+
10
+ desc "Creates a new Ember.js adapter"
11
+ class_option :ember_path, :type => :string, :aliases => "-d", :default => false, :desc => "Custom ember app path"
12
+ class_option :javascript_engine, :desc => "Engine for JavaScripts"
13
+ class_option :app_name, :type => :string, :aliases => "-n", :default => false, :desc => "Custom ember app name"
14
+
15
+ def create_adapter_files
16
+ file_path = File.join(ember_path, 'adapters', class_path, "#{file_name}_adapter.#{engine_extension}")
17
+ template "adapter.#{engine_extension}", file_path
18
+ end
19
+ end
20
+ end
21
+ end
@@ -11,7 +11,8 @@ module Ember
11
11
 
12
12
  class_option :ember_path, :type => :string, :aliases => "-d", :default => false, :desc => "Custom ember app path"
13
13
  class_option :skip_git, :type => :boolean, :aliases => "-g", :default => false, :desc => "Skip Git keeps"
14
- class_option :javascript_engine, :desc => "Engine for JavaScripts"
14
+ class_option :javascript_engine, :desc => "Engine for JavaScripts (js for JavaScript, coffee for CoffeeScript, etc)"
15
+ class_option :app_name, :type => :string, :aliases => "-n", :default => false, :desc => "Custom ember app name"
15
16
 
16
17
  def inject_ember
17
18
  begin
@@ -23,7 +24,7 @@ module Ember
23
24
 
24
25
 
25
26
  def create_dir_layout
26
- %W{models controllers views routes helpers templates}.each do |dir|
27
+ %W{models controllers views routes helpers components templates templates/components mixins adapters}.each do |dir|
27
28
  empty_directory "#{ember_path}/#{dir}"
28
29
  create_file "#{ember_path}/#{dir}/.gitkeep" unless options[:skip_git]
29
30
  end
@@ -37,17 +38,34 @@ module Ember
37
38
  template "router.#{engine_extension}", "#{ember_path}/router.#{engine_extension}"
38
39
  end
39
40
 
40
- def create_store_file
41
- template "store.#{engine_extension}", "#{ember_path}/store.#{engine_extension}"
41
+ def create_adapter_file
42
+ template "application_adapter.#{engine_extension}", "#{ember_path}/adapters/application_adapter.#{engine_extension}"
42
43
  end
43
44
 
44
45
  private
46
+
45
47
  def inject_into_application_file(safe_extension)
46
- application_file = "#{ember_path}/application.#{safe_extension}"
47
- inject_into_file( application_file, :before => /^.*require_tree.*$/) do
48
- context = instance_eval('binding')
49
- source = File.expand_path(find_in_source_paths("application.#{safe_extension}"))
50
- ERB.new(::File.binread(source), nil, '-', '@output_buffer').result(context)
48
+ application_file = "application.#{safe_extension}"
49
+ full_path = Pathname.new(destination_root).join(ember_path, application_file)
50
+
51
+ if full_path.exist?
52
+ contents = full_path.read
53
+ injection_options = if contents =~ regex = /^.*require_tree.*$/
54
+ {:before => regex}
55
+ elsif contents =~ regex = /^\s*$/
56
+ {:before => regex}
57
+ else
58
+ regex = /\z/
59
+ {:after => regex}
60
+ end
61
+
62
+ inject_into_file(full_path.to_s, injection_options) do
63
+ context = instance_eval('binding')
64
+ source = File.expand_path(find_in_source_paths(application_file))
65
+ ERB.new(::File.binread(source), nil, '-', '@output_buffer').result(context)
66
+ end
67
+ else
68
+ template application_file, full_path
51
69
  end
52
70
  end
53
71
  end
@@ -0,0 +1,28 @@
1
+ require 'generators/ember/generator_helpers'
2
+
3
+ module Ember
4
+ module Generators
5
+ class ComponentGenerator < ::Rails::Generators::NamedBase
6
+ include Ember::Generators::GeneratorHelpers
7
+
8
+ source_root File.expand_path("../../templates", __FILE__)
9
+
10
+ desc "Creates a new Ember.js component and component template\nCustom Ember Components require at least two descriptive names separated by a dash. Use CamelCase or dash-case to name your component.\n\nExample,\n\trails generate ember:component PostChart [options]\n\trails generate ember:component post-chart [options]"
11
+
12
+ class_option :javascript_engine, :desc => "Engine for JavaScripts"
13
+ class_option :ember_path, :type => :string, :aliases => "-d", :default => false, :desc => "Custom ember app path"
14
+ class_option :app_name, :type => :string, :aliases => "-n", :default => false, :desc => "Custom ember app name"
15
+
16
+ def create_component_files
17
+ dashed_file_name = file_name.gsub(/_/, '-')
18
+ comp_path = File.join(ember_path, 'components', class_path, "#{dashed_file_name}_component.#{engine_extension}")
19
+ template "component.#{engine_extension}", comp_path
20
+
21
+ templ_path = File.join(ember_path, 'templates/components', class_path, "#{dashed_file_name}.hbs")
22
+ template "component.template.hbs", templ_path
23
+
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -13,6 +13,7 @@ module Ember
13
13
  class_option :array, :type => :boolean, :default => false, :desc => "Create an Ember.ArrayController to represent multiple objects"
14
14
  class_option :ember_path, :type => :string, :aliases => "-d", :default => false, :desc => "Custom ember app path"
15
15
  class_option :object, :type => :boolean, :default => false, :desc => "Create an Ember.ObjectController to represent a single object"
16
+ class_option :app_name, :type => :string, :aliases => "-n", :default => false, :desc => "Custom ember app name"
16
17
 
17
18
 
18
19
  def create_controller_files
@@ -3,29 +3,56 @@ module Ember
3
3
  module GeneratorHelpers
4
4
 
5
5
  def ember_path
6
- options[:ember_path] || "app/assets/javascripts"
6
+ if options[:ember_path]
7
+ options[:ember_path]
8
+ elsif rails_engine?
9
+ "app/assets/javascripts/#{engine_name}"
10
+ elsif configuration.ember_path
11
+ configuration.ember_path
12
+ else
13
+ "app/assets/javascripts"
14
+ end
15
+ end
16
+
17
+ def rails_engine?
18
+ defined?(ENGINE_PATH)
19
+ end
20
+
21
+ def engine_name
22
+ ENGINE_PATH.split('/')[-2]
7
23
  end
8
24
 
9
25
  def application_name
10
- if defined?(::Rails) && ::Rails.application
26
+ if options[:app_name]
27
+ options[:app_name]
28
+ elsif rails_engine?
29
+ engine_name
30
+ elsif configuration.app_name
31
+ configuration.app_name
32
+ elsif defined?(::Rails) && ::Rails.application
11
33
  ::Rails.application.class.name.split('::').first
12
34
  else
13
- "app"
35
+ "App"
14
36
  end
15
37
  end
16
38
 
17
-
18
39
  def class_name
19
40
  (class_path + [file_name]).map!{ |m| m.camelize }.join()
20
41
  end
21
42
 
22
43
  def handlebars_template_path
23
- File.join(class_path, file_name).gsub(/^\//, '')
44
+ File.join(class_path, file_name).gsub(/^\//, '')
24
45
  end
25
46
 
26
47
  def engine_extension
27
48
  @engine_extension ||= "js.#{options[:javascript_engine]}".sub('js.js','js')
28
49
  end
50
+
51
+ def configuration
52
+ if defined?(::Rails) && ::Rails.configuration
53
+ ::Rails.configuration.ember
54
+ end
55
+ end
29
56
  end
30
57
  end
31
58
  end
@@ -3,39 +3,195 @@ require 'net/http'
3
3
  require 'uri'
4
4
  require 'fileutils'
5
5
 
6
+
6
7
  module Ember
7
8
  module Generators
8
9
  class InstallGenerator < ::Rails::Generators::Base
10
+
11
+ class InvalidChannel < ::Thor::Error; end
12
+ class ConflictingOptions < ::Thor::Error; end
13
+ class InsufficientOptions < ::Thor::Error; end
14
+
15
+ ::InvalidChannel = InvalidChannel
16
+ ::ConflictingOptions = ConflictingOptions
17
+ ::InsufficientOptions = InsufficientOptions
18
+
9
19
  desc "Install Ember.js into your vendor folder"
10
- class_option :head, :type => :boolean, :default => false, :desc => "Download latest Ember.js from GitHub and fetch it into your project"
20
+ class_option :head,
21
+ :type => :boolean,
22
+ :default => false,
23
+ :desc => "Download Ember.js & Ember data from canary channel. This is deprecated. Use channel instead."
24
+ class_option :channel,
25
+ :type => :string,
26
+ :required => false,
27
+ :desc => "Ember release channel Choose between 'release', 'beta' or 'canary'"
28
+ class_option :ember_only,
29
+ :type => :boolean,
30
+ :required => false,
31
+ :desc => "Only download Ember.",
32
+ :aliases => '--ember'
33
+ class_option :ember_data_only,
34
+ :type => :boolean,
35
+ :required => false,
36
+ :desc => "Only download ember-data",
37
+ :aliases => '--ember-data'
38
+ class_option :tag,
39
+ :type => :string,
40
+ :required => false,
41
+ :desc => "Download tagged release use syntax v1.0.0-beta.3/ember-data & v1.0.0-rc.8/ember"
42
+
43
+ def initialize(args = [], options = {}, config = {})
44
+ super(args, options, config)
45
+ check_options
46
+ process_options
47
+ end
48
+
49
+
50
+ def ember
51
+ begin
52
+ unless options.ember_data_only?
53
+ get_ember_js_for(:development)
54
+ get_ember_js_for(:production)
55
+ end
56
+ rescue Thor::Error
57
+ say('WARNING: no ember files on this channel or tag' , :yellow)
58
+ end
59
+ end
60
+
61
+ def ember_data
62
+ begin
63
+ unless options.ember_only?
64
+ get_ember_data_for(:development)
65
+ get_ember_data_for(:production)
66
+ end
67
+ rescue Thor::Error
68
+ say('WARNING: no ember-data files on this channel or tag' , :yellow)
69
+ end
70
+ end
71
+
72
+ private
73
+
74
+ def get_ember_data_for(environment)
75
+ # temporarily using a variable here until a stable release of
76
+ # ember-data is released so that installing with ember-data
77
+ # *just works*.
78
+ chan = if channel.to_s == 'release'
79
+ say_status("warning:", 'Ember Data is not available on the :release channel. Falling back to beta channel.' , :yellow)
80
+ :beta
81
+ else
82
+ channel
83
+ end
84
+
85
+ create_file "vendor/assets/ember/#{environment}/ember-data.js" do
86
+ fetch url_for(chan, 'ember-data', environment), "vendor/assets/ember/#{environment}/ember-data.js"
87
+ end
88
+
89
+ sourcemap_url = "#{base_url}/#{chan}/ember-data.js.map"
90
+ if resource_exist?(sourcemap_url)
91
+ create_file "vendor/assets/ember/#{environment}/ember-data.js.map" do
92
+ fetch sourcemap_url, "vendor/assets/ember/#{environment}/ember-data.js.map", false
93
+ end
94
+ end
95
+ end
11
96
 
12
- def fetch_ember
97
+ def get_ember_js_for(environment)
98
+ create_file "vendor/assets/ember/#{environment}/ember.js" do
99
+ fetch url_for(channel, 'ember', environment), "vendor/assets/ember/#{environment}/ember.js"
100
+ end
101
+ end
102
+
103
+ def url_for(channel, component, environment)
104
+ base = "#{base_url}/#{channel}/#{component}"
105
+
106
+ case environment
107
+ when :production
108
+ "#{base}.min.js"
109
+ when :development
110
+ if resource_exist?("#{base}.debug.js")
111
+ "#{base}.debug.js" # Ember.js 1.10.0.beta.1 or later
112
+ else
113
+ "#{base}.js"
114
+ end
115
+ end
116
+ end
117
+
118
+ def check_options
13
119
  if options.head?
14
- fetch 'http://builds.emberjs.com/ember-latest.js', 'vendor/assets/ember/development/ember.js'
15
- fetch 'http://builds.emberjs.com/ember-latest.min.js', 'vendor/assets/ember/production/ember.js'
120
+ say('WARNING: --head option is deprecated in favor of --channel=canary' , :yellow)
121
+ end
122
+ if options.head? && options.channel?
123
+ say 'ERROR: conflicting options. --head and --channel. Use either --head or --channel=<channel>', :red
124
+ raise ConflictingOptions
125
+ end
126
+ if options.channel? && !%w(release beta canary).include?(options[:channel])
127
+ say 'ERROR: channel can either be release, beta or canary', :red
128
+ raise InvalidChannel
129
+ end
130
+ if options.channel? && options.tag?
131
+ say 'ERROR: conflicting options. --tag and --channel. --tag is incompatible with other options', :red
132
+ raise ConflictingOptions
133
+ end
134
+ if options.head? && options.tag?
135
+ say 'ERROR: conflicting options. --tag and --head. --tag is incompatible with other options', :red
136
+ raise ConflictingOptions
137
+ end
138
+ if options.tag? && !(options.ember_only? || options.ember_data_only?)
139
+ say 'ERROR: insufficient options. --tag needs to be combined with eithe --ember or --ember-data', :red
140
+ raise InsufficientOptions
16
141
  end
17
142
  end
18
143
 
19
- def fetch_ember_data
144
+ def process_options
20
145
  if options.head?
21
- fetch 'http://builds.emberjs.com/ember-data-latest.js', 'vendor/assets/ember/development/ember-data.js'
22
- fetch 'http://builds.emberjs.com/ember-data-latest.min.js', 'vendor/assets/ember/production/ember-data.js'
146
+ @channel = :canary
147
+ end
148
+ if options.tag?
149
+ @channel = "tags/#{options.tag}"
23
150
  end
24
151
  end
25
152
 
26
- private
153
+ def base_url
154
+ 'http://builds.emberjs.com'
155
+ end
27
156
 
28
- def fetch(from, to)
157
+ def channel
158
+ if options.channel
159
+ @channel ||= options[:channel]
160
+ else
161
+ @channel ||= :release
162
+ end
163
+ end
164
+
165
+ def fetch(from, to, prepend_verbose = true)
29
166
  message = "#{from} -> #{to}"
30
167
  say_status("downloading:", message , :green)
31
168
 
32
169
  uri = URI(from)
170
+ output = StringIO.new
171
+ if prepend_verbose
172
+ output.puts "// Fetched from channel: #{channel}, with url " + uri.to_s
173
+ output.puts "// Fetched on: " + Time.now.utc.iso8601.to_s
174
+ end
33
175
 
34
- FileUtils.mkdir_p File.dirname(to)
35
-
36
- open(to, 'w+') do |output|
37
- output.puts Net::HTTP.get(uri).force_encoding("UTF-8")
176
+ response = Net::HTTP.get_response(uri)
177
+ case response.code
178
+ when '404'
179
+ say "ERROR: Error reading the content from the channel with url #{from}. File not found" , :red
180
+ raise Thor::Error
181
+ when '200'
182
+ output.puts response.body.force_encoding("UTF-8")
183
+ else
184
+ say "ERROR: Unexpected error with status #{response.code} reading the content from the channel with url #{from}." , :red
185
+ raise Thor::Error
38
186
  end
187
+ output.rewind
188
+ content = output.read
189
+ end
190
+
191
+ def resource_exist?(target)
192
+ uri = URI(target)
193
+ response = Net::HTTP.new(uri.host, uri.port).head(uri.path)
194
+ response.code == '200'
39
195
  end
40
196
  end
41
197
  end
@@ -10,6 +10,7 @@ module Ember
10
10
  argument :attributes, :type => :array, :default => [], :banner => "field[:type] field[:type] ..."
11
11
  class_option :javascript_engine, :desc => "engine for javascripts"
12
12
  class_option :ember_path, :type => :string, :aliases => "-d", :default => false, :desc => "custom ember app path"
13
+ class_option :app_name, :type => :string, :aliases => "-n", :default => false, :desc => "Custom ember app name"
13
14
 
14
15
  def create_model_files
15
16
  file_path = File.join(ember_path, 'models', class_path, "#{file_name}.#{engine_extension}")
@@ -13,6 +13,7 @@ module Ember
13
13
  class_option :skip_route, :type => :boolean, :default => false, :desc => "Don't create route"
14
14
  class_option :array, :type => :boolean, :default => false, :desc => "Create an Ember.ArrayController to represent multiple objects"
15
15
  class_option :object, :type => :boolean, :default => false, :desc => "Create an Ember.ObjectController to represent a single object"
16
+ class_option :app_name, :type => :string, :aliases => "-n", :default => false, :desc => "Custom ember app name"
16
17
 
17
18
 
18
19
  def create_resource_files
@@ -6,6 +6,12 @@ require "generators/ember/view_generator"
6
6
  module Rails
7
7
  module Generators
8
8
  ResourceGenerator.class_eval do
9
+
10
+ class_option :javascript_engine, :desc => "Engine for JavaScripts"
11
+ class_option :ember_path, :type => :string, :aliases => "-d", :default => false, :desc => "Custom ember app path"
12
+ class_option :with_template, :type => :boolean, :default => false, :desc => "Create template for this view"
13
+ class_option :app_name, :type => :string, :aliases => "-n", :default => false, :desc => "Custom ember app name"
14
+
9
15
  def add_ember
10
16
  say_status :invoke, "ember:model", :white
11
17
  with_padding do
@@ -14,7 +20,7 @@ module Rails
14
20
 
15
21
  say_status :invoke, "ember controller and view (singular)", :white
16
22
  with_padding do
17
- invoke "ember:view", [singular_name], :object => true
23
+ invoke "ember:view", [singular_name], options.merge(:object => true)
18
24
  end
19
25
 
20
26
  @_invocations[Ember::Generators::ControllerGenerator].delete "create_controller_files"
@@ -22,7 +28,7 @@ module Rails
22
28
 
23
29
  say_status :invoke, "ember controller and view (plural)", :white
24
30
  with_padding do
25
- invoke "ember:view", [plural_name], :array => true
31
+ invoke "ember:view", [plural_name], options.merge(:array => true)
26
32
  end
27
33
  end
28
34
  end
@@ -10,6 +10,7 @@ module Ember
10
10
  desc "Creates a new Ember.js route"
11
11
  class_option :ember_path, :type => :string, :aliases => "-d", :default => false, :desc => "Custom ember app path"
12
12
  class_option :javascript_engine, :desc => "Engine for JavaScripts"
13
+ class_option :app_name, :type => :string, :aliases => "-n", :default => false, :desc => "Custom ember app name"
13
14
 
14
15
  def create_route_files
15
16
  file_path = File.join(ember_path, 'routes', class_path, "#{file_name}_route.#{engine_extension}")