Ebook: Objects on Rails. Notes on flexible application design
Author: Grimm Avdi
- Genre: Computers // Programming: Programming Languages
- Tags: Библиотека, Компьютерная литература, Ruby / RoR
- Language: English
- zip
Objects on Rails. Revision 10
Copyright © 2012 by Avdi Grimm
197 pages (e-book: pdf, mobi, epub and source code of rails project)Introduction
Hi there! Welcome to Objects on Rails. This text is a step-by-step walkthrough
following the construction of a simple web application using Ruby
on Rails. It differs from other such walkthroughs in that it attempts to apply
a strongly Object-Oriented philosophy to the process.
Wait a sec! you say. Isn’t Rails already Object-Oriented? Well, yes
and no. While Rails is written in a thoroughly OO language, and built on
some solid OO patterns, there are aspects of conventional Rails application
development which depart significantly from OO practices.
Which is not necessarily a problem in and of itself. Lots of programs get by
just fine with (for instance) a mix of OO and Functional programming styles.
But experience has shown that these same Rails not-so-OO practices—such
as models which violate the Single Responsibility Principle (SRP), or putting
complex business logic in helpers—are a common source of development
delays in maturing Rails applications.
In this text we’ll build a basic blog application (yes, another one). You’ll be
my virtual pair-programmer as I work through the problems the application
presents by applying what I’ve learned of Object-Oriented design over the
years. I’ll do my best to explain my reasoning, step you through the code
piece by piece, and point you to further information about the patterns and
guidelines I’m following.
In some parts the Objects aspect of the text will just mean small tweaks
to standard Rails practices. In other areas we’ll take extended side-tracks
from the so-called golden path. Some techniques I apply will be tried-andand-
true chestnuts I use in every project I work on; others will be tools I only
haul out occasionally. In a few cases I’ll use this exercise as an opportunity
to work through ideas I haven’t had the opportunity to fully flesh out until
now. In a few cases that might mean we go down a dead-end siding; when
that happens I’ll let you know, and do my best to explain the thinking which
lead up to it.Contents:
About
Acknowledgments
Introduction
-What this is not
-About the approach
-About the Code
-A note on scale
-Why OOP?
Yet another frickin’ blog app
Adding blog entries
-Placeholder blog entries
-Making new entries
-Aside: subject and let
-Posts vs. Entries
-The Post class
-Why publish?
-Adding entries to the blog
Submitting posts
-Using ActiveModel
-The Post creation action
-Making the Blog object into a Singleton
-Object Trees and Lone Wolves
Getting the tests running again
-Stubbing out modules
Adding timestamps
-Sensible defaults for injected dependencies
OMG Dependency Injection!
-Injecting only the dependencies we need
Sorting and limiting posts
Adding validation
Introducing the Exhibit Pattern
-Exhibit A
-What about Presenters?
-Exhibit Object Characteristics
-Refactoring the exhibits
-Refactoring #exhibit
-Many models to many exhibits
-Do we need helpers?
Making the data stick around
-The trouble with ActiveRecord
-Melon Collie and the Infinite Protocol
-Adding ActiveRecord
-Why does Blog do the saving?
-Updating the Blog tests
-Separating out integration tests
-Updating the Post tests
-Mocking ActiveRecord
-Stubbing out the Database with NullDB
-Rake tasks for testing
-Using ActiveRecord objects as data access objects
Concealing ActiveRecord behind a FigLeaf
-Exiting Eden
Default content
-Navel-gazing objects
Exhibits for REST
Adding tags
-An Object Model for Tags
-Conversions
-Attaching the TagList to a Post
-Making Post more tag-aware
-Accepting and displaying tags
-Filtering posts by tag
-Extracting a Taggable role
-Using a mixin module
-Class-level Taggable methods
-Divesting Post of tagging
-Applying the Taggable role
-Refactoring to a separate ActiveRecord model
-Using ActiveRecord as a Row Data Gateway
-Constructing a TagStorage repository
-Putting on the finishing touches
-Reconsidering Taggable
Respecting controller privacy
Jealously guarding collections
-Objects as lending libraries
Toward self-rendering objects
-Exhibiting from inside the controller
-Exhibiting the blog object
-The exhibit_query macro
-Finishing the BlogExhibit
-Rendering the list of posts
-An exhibit for collections
-Transitive exhibited-ness
-Telling the post to render itself
-Entry collection, render thyself
-Rendering a TagList
-Bringing it all back home
Summary
-Scenarios
-You are beginning a new application
-Your need business objects that don’t exist yet in order to flesh
-out views
-You are writing view templates
-A view requires an ActiveRecord-style object in order to function
-correctly
-You are writing business models
-An object needs a way to make new instances of model
-A model collaborates with another model or a collection of
-other models
-A set of model methods are only needed in certain contexts
-You are writing unit tests. You want them to be fast and to
-enforce good encapsulation
-A non-ActiveRecord model needs to perform validations
-A model must be displayed using different HTML depending on
-its state
-You are developing a complex view with many models
-The logic matching Exhibits to models is getting complicated
-You need to output similar chunks of HTML in more than one
-view
-You need an object to persist between requests
-You need to persist a model to the database
-You need to test that a database query functions correctly
-A model needs to hook into various persistence lifecycle events
-You want to more fully separate your business models from ActiveRecord
-You want to add a child collection without adding a new table-
-You have replaced custom code with framework features,
-breaking the unit tests
-You want to display the same view with various different scopes
-applied to the model
-You want to expose a rich hypermedia RESTful API
Conclusion
-Feelin’ the burn
-TMTOWTDI
-at_exit
Appendix A: Further reading
-General
-Fast Tests and Mock Objects
-Data-Context-Interaction
-Presenters, decorators, and view models
-Views
-Rails
Appendix B: Acceptance Tests
Appendix C: Decoration vs. Dynamic Module Extension
-Composing an adventure
-Seeing in the dark
-A solution that’s all wet
-Modules to the rescue
-Other solutions
-Are decorators overrated?
-Conclusion
Appendix D: Test Helper Organization
-spec/spec_helper_lite.rb
-spec/spec_helper_nulldb.rb
-spec/spec_helper_full.rb
Credits
Copyright © 2012 by Avdi Grimm
197 pages (e-book: pdf, mobi, epub and source code of rails project)Introduction
Hi there! Welcome to Objects on Rails. This text is a step-by-step walkthrough
following the construction of a simple web application using Ruby
on Rails. It differs from other such walkthroughs in that it attempts to apply
a strongly Object-Oriented philosophy to the process.
Wait a sec! you say. Isn’t Rails already Object-Oriented? Well, yes
and no. While Rails is written in a thoroughly OO language, and built on
some solid OO patterns, there are aspects of conventional Rails application
development which depart significantly from OO practices.
Which is not necessarily a problem in and of itself. Lots of programs get by
just fine with (for instance) a mix of OO and Functional programming styles.
But experience has shown that these same Rails not-so-OO practices—such
as models which violate the Single Responsibility Principle (SRP), or putting
complex business logic in helpers—are a common source of development
delays in maturing Rails applications.
In this text we’ll build a basic blog application (yes, another one). You’ll be
my virtual pair-programmer as I work through the problems the application
presents by applying what I’ve learned of Object-Oriented design over the
years. I’ll do my best to explain my reasoning, step you through the code
piece by piece, and point you to further information about the patterns and
guidelines I’m following.
In some parts the Objects aspect of the text will just mean small tweaks
to standard Rails practices. In other areas we’ll take extended side-tracks
from the so-called golden path. Some techniques I apply will be tried-andand-
true chestnuts I use in every project I work on; others will be tools I only
haul out occasionally. In a few cases I’ll use this exercise as an opportunity
to work through ideas I haven’t had the opportunity to fully flesh out until
now. In a few cases that might mean we go down a dead-end siding; when
that happens I’ll let you know, and do my best to explain the thinking which
lead up to it.Contents:
About
Acknowledgments
Introduction
-What this is not
-About the approach
-About the Code
-A note on scale
-Why OOP?
Yet another frickin’ blog app
Adding blog entries
-Placeholder blog entries
-Making new entries
-Aside: subject and let
-Posts vs. Entries
-The Post class
-Why publish?
-Adding entries to the blog
Submitting posts
-Using ActiveModel
-The Post creation action
-Making the Blog object into a Singleton
-Object Trees and Lone Wolves
Getting the tests running again
-Stubbing out modules
Adding timestamps
-Sensible defaults for injected dependencies
OMG Dependency Injection!
-Injecting only the dependencies we need
Sorting and limiting posts
Adding validation
Introducing the Exhibit Pattern
-Exhibit A
-What about Presenters?
-Exhibit Object Characteristics
-Refactoring the exhibits
-Refactoring #exhibit
-Many models to many exhibits
-Do we need helpers?
Making the data stick around
-The trouble with ActiveRecord
-Melon Collie and the Infinite Protocol
-Adding ActiveRecord
-Why does Blog do the saving?
-Updating the Blog tests
-Separating out integration tests
-Updating the Post tests
-Mocking ActiveRecord
-Stubbing out the Database with NullDB
-Rake tasks for testing
-Using ActiveRecord objects as data access objects
Concealing ActiveRecord behind a FigLeaf
-Exiting Eden
Default content
-Navel-gazing objects
Exhibits for REST
Adding tags
-An Object Model for Tags
-Conversions
-Attaching the TagList to a Post
-Making Post more tag-aware
-Accepting and displaying tags
-Filtering posts by tag
-Extracting a Taggable role
-Using a mixin module
-Class-level Taggable methods
-Divesting Post of tagging
-Applying the Taggable role
-Refactoring to a separate ActiveRecord model
-Using ActiveRecord as a Row Data Gateway
-Constructing a TagStorage repository
-Putting on the finishing touches
-Reconsidering Taggable
Respecting controller privacy
Jealously guarding collections
-Objects as lending libraries
Toward self-rendering objects
-Exhibiting from inside the controller
-Exhibiting the blog object
-The exhibit_query macro
-Finishing the BlogExhibit
-Rendering the list of posts
-An exhibit for collections
-Transitive exhibited-ness
-Telling the post to render itself
-Entry collection, render thyself
-Rendering a TagList
-Bringing it all back home
Summary
-Scenarios
-You are beginning a new application
-Your need business objects that don’t exist yet in order to flesh
-out views
-You are writing view templates
-A view requires an ActiveRecord-style object in order to function
-correctly
-You are writing business models
-An object needs a way to make new instances of model
-A model collaborates with another model or a collection of
-other models
-A set of model methods are only needed in certain contexts
-You are writing unit tests. You want them to be fast and to
-enforce good encapsulation
-A non-ActiveRecord model needs to perform validations
-A model must be displayed using different HTML depending on
-its state
-You are developing a complex view with many models
-The logic matching Exhibits to models is getting complicated
-You need to output similar chunks of HTML in more than one
-view
-You need an object to persist between requests
-You need to persist a model to the database
-You need to test that a database query functions correctly
-A model needs to hook into various persistence lifecycle events
-You want to more fully separate your business models from ActiveRecord
-You want to add a child collection without adding a new table-
-You have replaced custom code with framework features,
-breaking the unit tests
-You want to display the same view with various different scopes
-applied to the model
-You want to expose a rich hypermedia RESTful API
Conclusion
-Feelin’ the burn
-TMTOWTDI
-at_exit
Appendix A: Further reading
-General
-Fast Tests and Mock Objects
-Data-Context-Interaction
-Presenters, decorators, and view models
-Views
-Rails
Appendix B: Acceptance Tests
Appendix C: Decoration vs. Dynamic Module Extension
-Composing an adventure
-Seeing in the dark
-A solution that’s all wet
-Modules to the rescue
-Other solutions
-Are decorators overrated?
-Conclusion
Appendix D: Test Helper Organization
-spec/spec_helper_lite.rb
-spec/spec_helper_nulldb.rb
-spec/spec_helper_full.rb
Credits
Download the book Objects on Rails. Notes on flexible application design for free or read online
Continue reading on any device:
Last viewed books
Related books
{related-news}
Comments (0)