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

Off The Tracks

Off The Tracks

Challenging The Rails Mindset. Overloaded classes, POROs, domain objects, form objects, view inheritance, representers, and more.

apotonick

June 18, 2013
Tweet

More Decks by apotonick

Other Decks in Programming

Transcript

  1. A

  2. class User < ActiveRecord::Base # do this... # do that..

    after_save :send_sms_notification end
  3. class User < ActiveRecord::Base # do this... # do that..

    after_save :send_sms_notification end
  4. A

  5. class User < ActiveRecord::Base # do this... # do that..

    after_save :send_sms_notification end
  6. class UsersController < Controller def create # ... @user.save notify_for(@user)

    end class UserSaveService def call user.save notify_for(user) end def notify_for(user)
  7. The Workflow. steve = Cowboy.new(name: "Steve") jim = Cowboy.new(name: "Jim")

    question = jim.say("Want some drink, Steve?") steve.respond("Oh, yes!", question)
  8. class Cowboy < OpenStruct ... def respond(what, question) msg =

    Response.new( content: what, to: question) chat << msg end end
  9. before do chat = [] jim = Cowboy.new(name: “Jim”, chat:

    chat) steve = ... qst = jim.say("Want some drink, Steve?") steve.respond("Oh, yes!", qst) end
  10. it “is in right order” do chat.to_readable.should == <<BONFIRE Two

    cowboys were sitting at a campfire. Jim said “Want some drink, Steve?” whereas Steve, who was already loaded as shit, responded “Oh, yes!” and both were happy to the end of their days. BONFIRE end
  11. “ “ /home/ Steve Jim Recent Messages “ “ Steve

    Jim 81% /dashboard/ /chat/show /stats/ /chat/delete
  12. app/views/shared/_sidebar.html.haml .box %h3 Recent Messages = # render list with

    @chat.messages .box - if draw_pie_chart? = pie_chart_for(@chat) - else = happiness_chart_for(@chat)
  13. class SidebarCell < Cell::Rails def show(chat) @chat = chat render

    end end app/cells/sidebar/show.html.haml .box %h3 Recent Messages = # render list with @chat.messages .box - if draw_pie_chart? = pie_chart_for(@chat) - else = happiness_chart_for(@chat)
  14. class Message def format self.upper end def to_s “<Message #{format}>”

    end end class BetterMessage < Message def format self.lower end end
  15. class MommyController < ApplicationContr.. def show ... render :partial =>

    'item' end end class ChildController < MommyController def show ... render :partial => 'item' end end views/child/_item.erb ? views/mommy/_item.erb ?
  16. app/cells/sidebar/show.html.haml .box %h3 Recent Messages = # render list with

    @chat.messages .box - if draw_pie_chart? = pie_chart_for(@chat) - else = happiness_chart_for(@chat)
  17. # objectify 1. POST /messages class IsDrinkerPolicy def allowed?(current_user) end

    class AddMessageService def call(current_user, params) end
  18. # objectify 1. POST /messages class IsDrinkerPolicy def allowed?(current_user) end

    class AddMessageService def call(current_user, params) end class MessageResponder def call end
  19. module MessageRepresenter include Roar::Representer::JSON::Collection version “2.0” href { messages_url(self) }

    items(class: Message) do property :content end link :self do message_url(self) end end
  20. [msg].extend(MessageRepresenter).to_json #=> {"collection" => { "version": "2.0", "href": "http://messages", "items":

    [ "data": [ {"name": "content", "value": "Want some drink, Steve?"}] "links" : {"self": {"href":"http://messages/rss"}} }