-
Notifications
You must be signed in to change notification settings - Fork 1
/
README
186 lines (134 loc) · 4.47 KB
/
README
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
ToDo's List Demo App
This app will be setup on Rails 3.1.1 and use sqlite3 for the db. My system is running Ruby 1.9.2.
1. If using RVM create a new gemset $ rvm use 1.9.2@todo --create
2. Install rails in gemset. $ gem install rails --no-ri --no-rdoc
3. $ rails new todo
4. Create .rvmrc file in project directory
rvm use 1.9.2@todo
5. cd into project directory $ cd todo
6. Start the rails server $ rails s
If you see the rails welcome page your app works! It does nothing but it works!
7. open your text editor $ mate .
8. Lets look at the guts of a rails app
-App
-Assets
+images
+javascripts
+stylesheets
-Controllers
-Helpers
-Mailers
-Models
-Views
-Config
+routes.rb
-DB
+this is where your migrations will be generated
-Gemfile
+ This is where you add gems to the project
-Gemfile.lock
+ When you run bundle this is where all the gems and version numbers get added in to
-Public
-no longer holds the assets
-index.html (the rails welcome page)
-test (the default test unit files)
9. Time to load up the Gemfile
gem 'kaminari'
group :development do
gem 'nifty-generators'
gem 'rspec-rails'
end
group :test do
gem 'rspec'
gem 'mocha'
end
10. Bundle the gems $ bundle
11. install rspec $ rails g rspec:install
In spec_helper.rb change Mock Framework to mocha
12. Start git $ git init
If you like git flow also run $ git flow init
13. Time to create our todo's
$ rails g scaffold task item:string description:text
$ rake db:migrate
$ rails s
go to localhost:3000/tasks
14. Make task#index the root view in routes.rb
uncomment root :to
Change 'welcome#index' to 'tasks#index'
Remove public/index.html
restart the server if necessary $ rails s
go to localhost:/3000 and you should see the items index page
15. Use nifty-generators to create basic layout
$ rails g nifty:layout
In application.html.erb change javascript_include_tag to :application
Rename /public/stylesheets/application.css to nifty.css
Move nifty.css to app/assest/stylesheets
Remove some extra stuff in the views/layouts/application.html.erb
Give the <title> a title
remove title helper from <body>
Reload the page & you should have a nice looking page on a blue background
16. Add add priority to tasks
$ rails g migration add_priority_to_tasks priority:integer
Look at migration and see rails magic
$ rake db:migrate
Add priority fields to views/tasks/_form.html.erb
<div class="field">
<%= f.label :priority %><br />
<%= f.text_field :priority %>
</div>
Add priority to views/tasks/show.html.erb
In views/tasks.index.html.erb change description to priority
make task.item a link to show = link_to task.item, task
remove show link
17. Scaffold generates a bunch of tests on it's own. You probably want to get rid of them. Lets add some tests of our own
First prepare the test db
$ rake db:test:prepare
spec/models/task_spec.rb
require 'spec_helper'
describe Task do
before(:each) do
@attr = {
:item => "test task",
:description => "test description",
:priority => 10
}
end
it "should create a new task given valid attributes" do
Task.create!(@attr)
end
describe "task validations" do
it "should require an item" do
Task.new(@attr.merge(:item =>""))
should_not be_valid
end
it "should require a priority" do
Task.new(@attr.merge(:priority =>""))
should_not be_valid
end
end
end
The task validations test we just added should fail. we will make them pass in the next step
$ rspec spec/models
18. Validate that the item & priority are not blank
models/task.rb
validates_presence_of :item, :priority
rspec should pass
19. Seeding the db
db/seeds.rb
1.upto(25) do |x|
Task.create(item: "Task #{x}", description: "Description #{x}", priority: x)
end
19. Making the default sort priority
models/task.rb
default_scope :order => 'tasks.priority DESC'
20. Adding pagination with kaminari
tasks_controller.rb
def index
@tasks = Task.order.page(params[:page])
end
tasks/index.html.haml above the table add
= paginate @tasks
Changing the number of items being shown
def index
@tasks = Task.order.page(params[:page]).per(10)
end