-
Notifications
You must be signed in to change notification settings - Fork 1
/
ember-list-data.rb
125 lines (108 loc) · 2.96 KB
/
ember-list-data.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
53
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
119
120
121
122
123
124
125
Dir[File.join(File.dirname(__FILE__), 'utils/*.rb')].each {|file| require file }
# rails new APP_PATH -m=https://raw.github.com/bazzel/rails-templates/master/ember-list-data.rb
# bundle exec rake rails:template LOCATION=https://raw.github.com/bazzel/rails-templates/master/ember-list-data.rb
#
# Tested with:
# Rails 4.0.2
# Ember 1.4.0-beta.5
# Ember Data 1.0.0-beta.6
#
# This templates asks for a model name and creates:
# - a Rails model
# - a Rails controller with an index action
# - some random data
# - an Ember route
# - an Ember template
#
puts
puts '== Rails template for setting up Ember.js ================================'
puts '-- see: http://emberjs.com/ and https://github.com/emberjs/ember-rails for more'
puts
# Declare and install gems
#
bundle_install do
gem 'faker'
end
#
# End Declare and install gems
# Collect model name
#
underscored = ask('What\'s the name of model (e.g. `Post` or `post`)?').underscore
pluralized = underscored.pluralize
model_name = underscored.camelize
model_names = model_name.pluralize
attributes = ask("Which attributes does '#{model_name}' have (e.g. `title:string body:text published:boolean`)?").downcase
# Generate resource and replace Rails controller
#
generate :resource, underscored, attributes
run "rm app/controllers/#{pluralized}_controller.rb"
file "app/controllers/#{pluralized}_controller.rb", <<-CODE
class #{model_names}Controller < ApplicationController
def index
render json: #{model_name}.all
end
end
CODE
# Add some fake data
#
# Try to figure out what fake data to use
parsed_attributes = attributes.split(' ').map do |attr|
name, type = attr.split(':')
value = case type
when 'text'
'Faker::Lorem.paragraphs.join(\'\\n\')'
when 'boolean'
'(rand(2) == 1)'
else
'Faker::Lorem.word'
end
"#{underscored}.#{name} = #{value}"
end
append_file 'db/seeds.rb', <<-CODE
#{model_name}.destroy_all
100.times do
#{model_name}.new.tap do |#{underscored}|
#{parsed_attributes.join("\n ")}
#{underscored}.save
end
end
CODE
rake 'db:migrate'
rake 'db:seed'
#
# End Add some fake data
# Ember stuff
#
# `rails g resource` already provided us with a Ember model
#
# Update the router manually:
inject_into_file app_js.join('router.js.coffee'), after: 'App.Router.map ()->' do
"\n @resource '#{pluralized}'"
end
# `rails g ember:resource` is not very useful:
file app_js.join("routes/#{pluralized}_route.js.coffee"), <<-CODE
App.#{model_names}Route = Ember.Route.extend
model: ->
@store.find '#{underscored}'
CODE
if ask('Do you need an application template? [yN]').downcase == 'y'
remove_file app_js.join('templates/application.handlebars')
file app_js.join('templates/application.handlebars'), <<-CODE
{{outlet}}
CODE
end
# Generate the template
#
file app_js.join("templates/#{pluralized}.handlebars"), <<-HTML
<ul>
{{#each}}
<li>{{title}}</li>
{{/each}}
</ul>
HTML
#
# End Ember stuff
puts
puts 'Start your server and navigate to:'
puts "http://localhost:3000##{pluralized}"
puts