Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Patterns Falacy - Rails Version

The Patterns Falacy - Rails Version

Some ideas about behaviors on rails community. People prefer chose rails patterns against think about your own problems, in your apps and solve them with new ideas.

Luciano Sousa

May 25, 2013
Tweet

More Decks by Luciano Sousa

Other Decks in Programming

Transcript

  1. Focus on ideas, not examples Thing about what you are

    you doing with your apps Try solve your problems out of the box Monday, May 27, 13
  2. MVC Definition Model–view–controller (MVC) is a software architecture pattern which

    separates the representation of information from the user's interaction with it. (...) • A controller can send commands to its associated view to change the view's presentation of the model (e.g., by scrolling through a document). It can also send commands to the model to update the model's state (e.g., editing a document). • A model notifies its associated views and controllers when there has been a change in its state. This notification allows the views to produce updated output, and the controllers to change the available set of commands (...). • A view requests from the model the information that it needs to generate an output representation to the user. http://en.wikipedia.org/wiki/Model-view-controller Monday, May 27, 13
  3. class User < ActiveRecord::Base has_many :posts def post_titles posts.map(&:title).join(', ')

    end end class Post < ActiveRecord::Base belongs_to :user end Monday, May 27, 13
  4. module PostsExtensions def post_titles names = posts.map(&:title) names.join(‘, ‘) end

    end class User < ActiveRecord::Base include PostsExtensions has_many :posts end Monday, May 27, 13
  5. class UsersController < ApplicationController def index @users = search_users end

    private def search_users filters = [] unless params[:name].blank? filters << [ “name = ?”, params[:name] end unless params.[:last_name].blank? filters << [ “last_name = ?”, params[: last_name] end . . . User.where(filters) end end Monday, May 27, 13
  6. module UsersSearch def search_users filters = [] unless params[:name].blank? filters

    << [ “name = ?”, params[:name] end unless params.[:last_name].blank? filters << [ “last_name = ?”, params[: last_name] end . . . User.where(filters) end end Monday, May 27, 13
  7. class PostsList def initialize(posts, user) @posts = posts @user =

    user end def build_posts posts_list = [] @posts do |post| posts_list << OpenStruct.new( title: post.title, description: post.description ? post.description : ‘Sorry. No text here!’, author_name: @user.full_name ) end posts_list end Monday, May 27, 13
  8. <% @posts.each do |post| %> <%= post.title %> <%= post.

    description %> <%= post.author_name %> <% end %> Monday, May 27, 13
  9. class User < ActiveRecord::Base include PostsExtensions include AvatarsExtensions include PaymentsExtensions

    include include include include include include include . . . end Monday, May 27, 13
  10. class Registrations < ActiveRecord::Base include UsersExtensions has_many :users end >

    r = Registration.first > r.users => [ ‘you’, ‘are dumb’] Monday, May 27, 13
  11. sum = any_hash.map { |k, v| k + v *

    10 } Monday, May 27, 13
  12. require 'spec_helper' describe User do it 'testing create user time'

    do 100.times { user = FactoryGirl.create(:user) user.should_not be_nil } end end Monday, May 27, 13
  13. require 'spec_helper' describe User do it 'testing create user time'

    do 100.times { user = double(“User”) user.should_not be_nil } end end Monday, May 27, 13