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

Patterns Falacy v2

Patterns Falacy v2

My second talk about 'patterns' on rails community. Some of them should be updated to avoid scale problems and others stuffs.

Luciano Sousa

May 17, 2014
Tweet

More Decks by Luciano Sousa

Other Decks in Technology

Transcript

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

    you doing with your apps Try solve your problems out of the box
  2. MVC

  3. 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
  4. class User < ActiveRecord::Base has_many :posts ! def post_titles posts.map(&:title).join(',

    ') end end ! class Post < ActiveRecord::Base belongs_to :user end
  5. module PostsExtensions def post_titles names = posts.map(&:title) names.join(‘, ‘) end

    end class User < ActiveRecord::Base include PostsExtensions ! has_many :posts end
  6. 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
  7. 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
  8. <% if @user.citizenship %> <%= some_helper @user.citizenship %> <% else

    %> <%= content_tag :span, t(‘not_related_citizenship’) %> <% end %>
  9. 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 || ‘Sorry. No text here!’, author_name: @user.full_name ) end ! posts_list end
  10. Class PostsController < ApplicationController include PostsList ! def index @posts

    = PostList.new(Post.recent, current_user).build_posts end end
  11. <% @posts.each do |post| %> <%= post.title %> <%= post.

    description %> <%= post.author_name %> <% end %>
  12. class Registrations < ActiveRecord::Base include UsersExtensions ! has_many :users end

    > r = Registration.first > r.users => [ ‘you’, ‘are dumb’]
  13. require 'spec_helper' ! describe User do it 'testing create user

    time' do 100.times { user = create(:user) user.should_not be_nil } end end
  14. require 'spec_helper' ! describe User do it 'testing create user

    time' do 100.times { user = double(“User”) user.should_not be_nil } end end