example rails application - mit opencourseware · 2020-01-04 · what are resources? › projects,...

31
software studio Example Rails Application  Eunsuk Kang  1

Upload: others

Post on 13-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

softwarestudio Example Rails Application 

Eunsuk Kang  1

Page 2: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

Demo: An app for collaboration 

2

Page 3: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

MVC Design 

Controller

ModelView

3

Page 4: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

Exercise: What’s in a Model? 

What are resources? 

What are their attributes? Constraints? 

What about relationships between them? 

4

Page 5: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

Resources

What are resources? 

5

Page 6: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

Resources

What are resources?  › Projects, comments 

6

Page 7: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

Attributes

What are resources? › Projects, comments

What are their attributes? Constraints? 

7

Page 8: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

Attributes

What are resources? › Projects, comments

What are their attributes? Constraints? › Each project has a title & owner’s e-mail › Each project is accessible with a secret key › Each comment has a commenter and a body

8

Page 9: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

Relationships

What are resources? › Projects, comments

What are their attributes? Constraints? › Each project has a title & owner’s e-mail › Each project is accessible with a secret key › Each comment has a commenter and a body

What about relationships between them?

9

Page 10: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

Relationships

What are resources? › Projects, comments

What are their attributes? Constraints? › Each project has a title & owner’s e-mail › Each project is accessible with a secret key › Each comment has a commenter and a body

What about relationships between them? › A project has many comments

10

Page 11: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

Rails Convention 

projects/ project.rb show.html.erb comments.rb comments/_comment.html.erb ...

projects_controller.rb comments_controller.rb ... 

Controller

ModelView

11

Page 12: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

Model 

projects/ project.rb show.html.erb comments.rb comments/_comment.html.erb ...

projects_controller.rb comments_controller.rb ...

Controller

ModelView

12

Page 13: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

ActiveRecords  app/models/project.rb  class Project < ActiveRecord::Base

KEY_LENGTH = 10 VALID_EMAIL_REGEX = /\A[\w+\-­‐.]+@[a-­‐z\d\-­‐.]+\.[a-­‐z]+\z/i

attr_accessible :title, :owner_email before_create :generate_access_key

validates :title, :presence => true validates :owner_email, :presence => true,

:format => { :with => VALID_EMAIL_REGEX }

has_many :comments

def generate_access_key self.access_key = SecureRandom.hex(KEY_LENGTH)

end end

13

Page 14: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

Validation Enforce constraints over your data!  class Project < ActiveRecord::Base

KEY_LENGTH = 10 VALID_EMAIL_REGEX = /\A[\w+\-­‐.]+@[a-­‐z\d\-­‐.]+\.[a-­‐z]+\z/i

attr_accessible :title, :owner_email before_create :generate_access_key

validates :title, :presence => true validates :owner_email, :presence => true,

:format => { :with => VALID_EMAIL_REGEX }

has_many :comments

def generate_access_key self.access_key = SecureRandom.hex(KEY_LENGTH)

end end

14

Page 15: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

Callbacks Methods called at various points  class Project < ActiveRecord::Base

KEY_LENGTH = 10 VALID_EMAIL_REGEX = /\A[\w+\-­‐.]+@[a-­‐z\d\-­‐.]+\.[a-­‐z]+\z/i

attr_accessible :title, :owner_email before_create :generate_access_key

validates :title, :presence => true validates :owner_email, :presence => true,

:format => { :with => VALID_EMAIL_REGEX }

has_many :comments

def generate_access_key self.access_key = SecureRandom.hex(KEY_LENGTH)

end end

15

Page 16: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

Associations Declare relationships between objects  class Project < ActiveRecord::Base

... has_many :comments ...

end

class Comment < ActiveRecord::Base belongs_to :project ...

end

To create a new comment for a project:  @comment = @project.comments.create(

:commenter => ‘Alex’, :body => ‘Hello World!’)

16

Page 17: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

DB Schema  db/schema.rb ActiveRecord::Schema.define(:version => 20130203203925) do

create_table "projects", :force => true do |t| t.string "title" t.string "owner_email" t.string "access_key"

end

create_table "comments", :force => true do |t| t.string "commenter" t.text "body" t.string "project_id"

end

end

17

Page 18: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

Migration Create DB table from schema definition  ActiveRecord::Schema.define(:version => 20130203203925) do

create_table "projects", :force => true do |t| t.string "title" t.string "owner_email" t.string "access_key"

end ...

end

id title owner_email access_key

1 “6.170 Homework #1” e5056b6f653214f4

2 “House utility bills” [email protected] 24fa027bdb6794e

18

[email protected]

Page 19: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

View 

projects/ project.rb show.html.erb comments.rb comments/_comment.html.erb ...

projects_controller.rb comments_controller.rb

Controller

ModelView

... 19

Page 20: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

View Templates app/views/projects/show.html.erb <table> <tr> <td> <div id="comments""comments"> <div class="comment""comment"> <i>Welcome to <%= @project..title %>!</i><br> <i>Your comment goes here...</i><br><br>

</div> <%= render @project.comments %>

</div> </td> </tr> </table>

20

Page 21: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

Partials Factor out & reuse a partial view (‘Don’t Repeat Yourself’) <table> <tr> <td> <div id="comments""comments"> ... <%= render @project.comments %>

</div> </td> </tr> </table>

app/views/comments/_comment.html.erb <div class="comment""comment"> <b><%= comment.commenter %></b>: <%= comment.body %>

</div>

21

Page 22: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

Controller

projects/ project.rb show.html.erb comments.rb comments/_comment.html.erb ...

projects_controller.rb comments_controller.rb ...

Controller

ModelView

22

Page 23: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

Routes config/routes.rb Engage::Application.routes.draw do

... match 'projects/:id' => 'projects#show', :via => :get ...

end

Example › Request GET /projects/bce7cca9ee32 › Controller Method ProjectsController.show (with id = bce7cca9ee32)

23

Page 24: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

Controllers app/controllers/projects_controller.rb class ProjectsControlle << ApplicationController

defdef show ## Look up project

@project Project.Project.find(params[:id])

if no @[email protected]? @username = cookiescookies[:current_username]

respond_to do |formatformat| format.html # show.html.erb format.json { render :json = @project@project }

end else

render_404 end

end ...

end 24

Page 25: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

DB Access No SQL queries; use Rails helpers class ProjectsControlle << ApplicationController

defdef show ## Look up project

@project Project.Project.find(params[:id])

i notnot @project.blank? @username = cookies[:current_username]

respond_to do |formatformat| format.html # show.html.erb format.json { render :json => @project }

end else

render_404 end

end ...

end 25

Page 26: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

DB Access Look up by access_key, not ID! class ProjectsControlle << ApplicationController

defdef show ## Look up project by access_key

@project Project.Project.find_by_access_key(params[:id])

i notnot @project.blank? @username = cookies[:current_username]

respond_to do |formatformat| format.html # show.html.erb format.json { render :json => @project }

end else

render_404 end

end ...

end 26

Page 27: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

Response Multiple response formats class ProjectsControlle << ApplicationController

defdef show ## Look up project by access_key

@project Project.Project.find_by_access_key(params[:id])

if no @[email protected]? @username = cookies[:current_username]

respond_to do |formatformat| format.html # show.html.erb format.json { render :json = @project@project }

end else

render_404 end

end ...

end 27

Page 28: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

Default Routes Rails generates CRUD routes (think REST!) Engage::Application.routes.draw do

... # match 'projects/:id' => 'projects#show', :via => :get resources :projects ...

end

console> rake routes

GET /projects projects#index POST /projects projects#create GET /projects/new projects#new GET /projects/:id/edit projects#edit GET /projects/:id projects#show PUT /projects/:id projects#update DELETE /projects/:id projects#destroy

28

Page 29: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

Nested Routes

Engage::Application.routes.draw do ... resources :projects do

resourcesresources :comments end ...

end

console> rake routes ... GET /projects/:project_id/comments comments#index POST /projects/:project_id/comments comments#create GET /projects/:project_id/comments/new comments#new GET /projects/:project_id/comments/:id/edit comments#edit ...

29

Page 30: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

Code on Github

New features added over term › Collaborative editing, uploads, user accounts, etc. › Released as separate branches

30

Page 31: Example Rails Application - MIT OpenCourseWare · 2020-01-04 · What are resources? › Projects, comments . What are their attributes? ... 1 “6.170 Homework #1” e5056b6f653214f4

MIT OpenCourseWarehttp://ocw.mit.edu

6.170 Software StudioSpring 2013

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.