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

Escrevendo Aplicações Melhores @ RubyConf Brasil

Escrevendo Aplicações Melhores @ RubyConf Brasil

77237b97a465ae5a293ad323b7296837?s=128

Carlos Antonio

September 05, 2012
Tweet

Transcript

  1. Active Model l escrevendo aplicações melhores com

  2. http://pragprog.com/book/ppmetr/metaprogramming-ruby

  3. Rails 2.3.2

  4. Active Record

  5. Validações

  6. obj = MyClass.new obj.attr = 'test' puts obj.valid? # =>

    true obj.attr = 'tst' puts obj.valid? # => false
  7. require 'activerecord' ActiveRecord::Base # autoload all modules class MyClass def

    save; end def save!; end def new_record?; true; end include ActiveRecord::Validations attr_accessor :attr validates_length_of :attr, :minimum => 4 end
  8. “I had to go through some hoops to make this

    code work.” Paolo Perrotta Metaprogramming Ruby
  9. require 'activerecord' ActiveRecord::Base # autoload all modules class MyClass def

    save; end def save!; end def new_record?; true; end include ActiveRecord::Validations attr_accessor :attr validates_length_of :attr, :minimum => 4 end What? ActiveRecord::Base # autoload all modules
  10. require 'activerecord' ActiveRecord::Base # autoload all modules class MyClass def

    save; end def save!; end def new_record?; true; end include ActiveRecord::Validations attr_accessor :attr validates_length_of :attr, :minimum => 4 end def save; end def save!; end LOL
  11. require 'activerecord' ActiveRecord::Base # autoload all modules class MyClass def

    save; end def save!; end def new_record?; true; end include ActiveRecord::Validations attr_accessor :attr validates_length_of :attr, :minimum => 4 end def new_record?; true; end trolling
  12. require 'activerecord' ActiveRecord::Base # autoload all modules class MyClass def

    save; end def save!; end def new_record?; true; end include ActiveRecord::Validations attr_accessor :attr validates_length_of :attr, :minimum => 4 end include ActiveRecord::Validations attr_accessor :attr validates_length_of :attr, :minimum => 4 there we go!
  13. “However, that wasn’t too much work in exchange for a

    flexible set of validation Class Macros.” Paolo Perrotta Metaprogramming Ruby
  14. work? not too much

  15. require 'activerecord' ActiveRecord::Base # autoload all modules class MyClass def

    save; end def save!; end def new_record?; true; end include ActiveRecord::Validations attr_accessor :attr validates_length_of :attr, :minimum => 4 end ActiveRecord::Base # autoload all modules def save; end def save!; end def new_record?; true; end
  16. require 'activerecord' ActiveRecord::Base # autoload all modules class MyClass def

    save; end def save!; end def new_record?; true; end include ActiveRecord::Validations attr_accessor :attr validates_length_of :attr, :minimum => 4 end include ActiveRecord::Validations attr_accessor :attr validates_length_of :attr, :minimum => 4
  17. work! too much

  18. nisso? como chegar

  19. Source diving!

  20. Active Model

  21. @cantoniodasilva Carlos Antonio

  22. None
  23. Devise maintainer SimpleForm maintainer

  24. Rails commiter

  25. guru SC

  26. Active Model aplicações melhores escrevendo com

  27. Rails 2.3

  28. Active Record VALIDAÇÕES SERIALIZAÇÃO JSON/XML ...

  29. Active Resource VALIDAÇÕES SERIALIZAÇÃO JSON/XML ...

  30. Mongoid VALIDAÇÕES ? SERIALIZAÇÃO ? ...

  31. MyClass VALIDAÇÕES ? SERIALIZAÇÃO ? ...

  32. require 'activerecord' ActiveRecord::Base # autoload all modules class MyClass def

    save; end def save!; end def new_record?; true; end include ActiveRecord::Validations attr_accessor :attr validates_length_of :attr, :minimum => 4 end
  33. ! DRY

  34. Frágil

  35. Rails 3

  36. Rails 3 Rewrite Everything!

  37. Active Model

  38. Active Model VALIDAÇÕES SERIALIZAÇÃO JSON/XML ...

  39. Active Record include ActiveModel::*

  40. module ActiveRecord class Base extend ActiveModel::Naming # Uses ActiveModel::Translation extend

    Translation # Uses ActiveModel::MassAssignmentSecurity include AttributeAssignment include ActiveModel::Conversion # Uses ActiveModel::Validations include Validations # Uses ActiveModel::AttributeMethods include AttributeMethods # Uses ActiveModel::Callbacks include Callbacks include ActiveModel::Observing include ActiveModel::SecurePassword # Uses ActiveModel::Serialization include Serialization # ... end end
  41. Active Resource include ActiveModel::*

  42. module ActiveResource class Base extend ActiveModel::Naming # Uses ActiveModel::Observing include

    Observing # Uses ActiveModel::Validations include Validations include ActiveModel::Conversion include ActiveModel::Serializers::JSON include ActiveModel::Serializers::Xml # ... end end
  43. works how it

  44. Validations

  45. class Report < ActiveRecord::Base validates_presence_of :name end Active Record

  46. report = Report.new puts report.valid? #=> false puts report.errors.full_messages #=>

    Name can't be blank Active Record
  47. class Report include ActiveModel::Validations attr_accessor :name validates_presence_of :name end Active

    Model
  48. class Report include ActiveModel::Validations attr_accessor :name validates_presence_of :name end Active

    Model include ActiveModel::Validations validates_presence_of :name
  49. report = Report.new puts report.valid? #=> false puts report.errors.full_messages #=>

    Name can't be blank Active Model
  50. Validator

  51. class Report < ActiveRecord::Base validates_with ReportValidator end Active Record

  52. class ReportValidator < ActiveModel::Validator def validate(report) if report.name.blank? report.errors.add :name,

    :blank end # More validations... end end Active Record
  53. class ReportValidator < ActiveModel::Validator def validate(report) if report.name.blank? report.errors.add :name,

    :blank end # More validations... end end Active Record ReportValidator < ActiveModel::Validator def validate(report)
  54. report = Report.new puts report.valid? #=> false puts report.errors.full_messages #=>

    Name can't be blank Active Record
  55. class Report include ActiveModel::Validations validates_with ReportValidator attr_accessor :name end Active

    Model
  56. class Report include ActiveModel::Validations validates_with ReportValidator attr_accessor :name end Active

    Model include ActiveModel::Validations validates_with ReportValidator
  57. report = Report.new puts report.valid? #=> false puts report.errors.full_messages #=>

    Name can't be blank Active Model
  58. Validations: Nova Sintaxe

  59. class Report include ActiveModel::Validations attr_accessor :name # validates_presence_of :name validates

    :name, presence: true end Active Model
  60. class Report include ActiveModel::Validations attr_accessor :name # validates_presence_of :name validates

    :name, presence: true end Active Model include ActiveModel::Validations validates :name, presence: true
  61. Custom Validator

  62. class Report include ActiveModel::Validations attr_accessor :no_name validates :no_name, absence: true

    end Active Model
  63. class Report include ActiveModel::Validations attr_accessor :no_name validates :no_name, absence: true

    end include ActiveModel::Validations validates :no_name, absence: true Active Model
  64. class AbsenceValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) if value.present?

    record.errors.add attribute, :absence end end end Active Model
  65. class AbsenceValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) if value.present?

    record.errors.add attribute, :absence end end end AbsenceValidator Active Model
  66. class AbsenceValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) if value.present?

    record.errors.add attribute, :absence end end end AbsenceValidator Active Model AbsenceValidator < ActiveModel::EachValidator def validate_each(record, attribute, value)
  67. Translation

  68. # I18n: activerecord: attributes: report: name: 'Nome' puts Report.human_attribute_name('name') #=>

    "Nome" Active Record
  69. # I18n: activemodel: attributes: report: name: 'Nome' Active Model

  70. # I18n: activemodel: attributes: report: name: 'Nome' activemodel: Active Model

  71. class Report extend ActiveModel::Translation end puts Report.human_attribute_name('name') #=> "Nome" Active

    Model
  72. Validations + Translation

  73. # I18n: activemodel: attributes: report: name: 'Nome' errors: messages: blank:

    'deve ser preenchido' Active Model
  74. report = Report.new puts report.valid? #=> false puts report.errors.full_messages #=>

    Nome deve ser preenchido Active Model
  75. Callbacks

  76. class Report < ActiveRecord::Base before_save { puts "it works before"

    } after_save { puts "it works after as well" } end Active Record
  77. Report.new.save #=> it works before #=> ...saving... #=> it works

    after as well Active Record
  78. class Report extend ActiveModel::Callbacks define_model_callbacks :save before_save { puts "it

    works before" } after_save { puts "it works after as well" } def save run_callbacks :save do puts "saving..." end end end Active Model
  79. class Report extend ActiveModel::Callbacks define_model_callbacks :save before_save { puts "it

    works before" } after_save { puts "it works after as well" } def save run_callbacks :save do puts "saving..." end end end extend ActiveModel::Callbacks define_model_callbacks :save before_save { puts "it works before" } after_save { puts "it works after as well" } run_callbacks :save do puts "saving..." end Active Model
  80. Report.new.save #=> it works before #=> ...saving... #=> it works

    after as well Active Model
  81. Serialization

  82. report = Report.new report.name = "Omglol" puts report.to_json #=> {"report":{"name":"Omglol"}}

    Active Record
  83. class Report extend ActiveModel::Naming include ActiveModel::Serializers::JSON attr_accessor :name def attributes

    { name: name } end end Active Model
  84. class Report extend ActiveModel::Naming include ActiveModel::Serializers::JSON attr_accessor :name def attributes

    { name: name } end end Active Model include ActiveModel::Serializers::JSON def attributes { name: name } end
  85. report = Report.new report.name = "Omglol" puts report.to_json #=> {"report":{"name":"Omglol"}}

    Active Model
  86. report = Report.new report.name = "Omglol" puts report.to_xml #=> <?xml

    version="1.0" encoding="UTF-8"?> #=> <report> #=> <name>Omglol</name> #=> </report> Active Record
  87. class Report extend ActiveModel::Naming include ActiveModel::Serializers::Xml attr_accessor :name def attributes

    { name: name } end end Active Model
  88. class Report extend ActiveModel::Naming include ActiveModel::Serializers::Xml attr_accessor :name def attributes

    { name: name } end end include ActiveModel::Serializers::Xml def attributes { name: name } end Active Model
  89. report = Report.new report.name = "Omglol" puts report.to_xml #=> <?xml

    version="1.0" encoding="UTF-8"?> #=> <report> #=> <name>Omglol</name> #=> </report> Active Model
  90. Attribute Methods

  91. report = Report.new puts report.name? #=> false report.name = "Omglol"

    puts report.name? #=> true Active Record
  92. report = Report.new puts report.name? #=> false report.name = "Omglol"

    puts report.name? #=> true report.name? #=> false report.name = "Omglol" report.name? #=> true Active Record
  93. class Report include ActiveModel::AttributeMethods attribute_method_suffix '?' define_attribute_methods [:name] attr_accessor :name

    private def attribute?(attr) send(attr).present? end end Active Model
  94. class Report include ActiveModel::AttributeMethods attribute_method_suffix '?' define_attribute_methods [:name] attr_accessor :name

    private def attribute?(attr) send(attr).present? end end include ActiveModel::AttributeMethods attribute_method_suffix '?' define_attribute_methods [:name] def attribute?(attr) send(attr).present? end Active Model
  95. report = Report.new puts report.name? #=> false report.name = "Omglol"

    puts report.name? #=> true Active Model
  96. report = Report.new puts report.name? #=> false report.name = "Omglol"

    puts report.name? #=> true report.name? #=> false report.name = "Omglol" report.name? #=> true Active Model
  97. class Report include ActiveModel::AttributeMethods attribute_method_prefix 'clear_' define_attribute_methods [:name] attr_accessor :name

    private def clear_attribute(attr) send "#{attr}=", nil end end Active Model
  98. class Report include ActiveModel::AttributeMethods attribute_method_prefix 'clear_' define_attribute_methods [:name] attr_accessor :name

    private def clear_attribute(attr) send "#{attr}=", nil end end Active Model include ActiveModel::AttributeMethods attribute_method_prefix 'clear_' define_attribute_methods [:name] def clear_attribute(attr) send "#{attr}=", nil end
  99. report.name = "Omglol" puts report.name? #=> true report.clear_name puts report.name?

    #=> false Active Model
  100. report.name = "Omglol" puts report.name? #=> true report.clear_name puts report.name?

    #=> false report.name? #=> true report.clear_name report.name? #=> false Active Model
  101. DRY !

  102. Active Model <3 <3 <3

  103. isso... Enquanto

  104. 2.3.OMG Rails:

  105. Invitation Feature:

  106. class Invitation attr_accessor :contacts, :message end

  107. <% form_for @invitation do |f| %> ... <% end %>

  108. None
  109. WTF!?!

  110. <form action="/invitations/%23%3CInvitation: 0x1060c2b08%3E" class="edit_invitation" id="edit_invitation_2198214020" method="post"> ... </form>

  111. <form action="/invitations/%23%3CInvitation: 0x1060c2b08%3E" class="edit_invitation" id="edit_invitation_2198214020" method="post"> ... </form> action="/invitations/%23%3CInvitation: 0x1060c2b08%3E"

    What?
  112. <form action="/invitations/%23%3CInvitation: 0x1060c2b08%3E" class="edit_invitation" id="edit_invitation_2198214020" method="post"> ... </form> class="edit_invitation" edit?

    LOL
  113. <form action="/invitations/%23%3CInvitation: 0x1060c2b08%3E" class="edit_invitation" id="edit_invitation_2198214020" method="post"> ... </form> id="edit_invitation_2198214020" troll

    number
  114. class Invitation attr_accessor :contacts, :message def new_record? true end def

    id nil end end
  115. class Invitation attr_accessor :contacts, :message def new_record? true end def

    id nil end end def new_record? true end def id nil end
  116. <form action="/invitations" class="new_invitation" id="new_invitation" method="post"> ... </form> there we go!

  117. nisso? como chegar

  118. Source diving!

  119. Rails 2.3

  120. Action Pack Active Record

  121. Active Record? Blargh!?!!

  122. NoSQL FTW!!

  123. Action Pack Mongoid

  124. monkey Patch

  125. Outro objeto em um form?

  126. monkey Patch

  127. class Invitation attr_accessor :contacts, :message def new_record? true end def

    id nil end end def new_record? true end def id nil end
  128. ! API

  129. Frágil

  130. Rails 3

  131. Rails 3 Rewrite Everything!

  132. Active Model

  133. Action Pack Active Model

  134. Active Model Active Record Action Pack

  135. Active Model Mongoid Action Pack

  136. Outro objeto em um form?

  137. Active Model OmgLol Action Pack

  138. works how it

  139. form_for @report do |f| f.label :name f.text_field :name f.submit end

  140. <form action="/reports" class="new_report" id="new_report" method="post"> <label for="report_name">Nome</label> <input id="report_name" size="30"

    name="report[name]" type="text" /> <input name="commit" type="submit" value="Criar Relatório" /> </form>
  141. <form action="/reports" class="new_report" id="new_report" method="post"> <label for="report_name">Nome</label> <input id="report_name" size="30"

    name="report[name]" type="text" /> <input name="commit" type="submit" value="Criar Relatório" /> </form> <form action="/reports" class="new_report" id="new_report" method="post"> como?
  142. <form action="/reports" class="new_report" id="new_report" method="post"> <label for="report_name">Nome</label> <input id="report_name" size="30"

    name="report[name]" type="text" /> <input name="commit" type="submit" value="Criar Relatório" /> </form> <form action="/reports" class="new_report" id="new_report" method="post"> como?
  143. <form action="/reports" class="new_report" id="new_report" method="post"> <label for="report_name">Nome</label> <input id="report_name" size="30"

    name="report[name]" type="text" /> <input name="commit" type="submit" value="Criar Relatório" /> </form> <form action="/reports" class="new_report" id="new_report" method="post"> como?
  144. <form action="/reports" class="new_report" id="new_report" method="post"> <label for="report_name">Nome</label> <input id="report_name" size="30"

    name="report[name]" type="text" /> <input name="commit" type="submit" value="Criar Relatório" /> </form> <label for="report_name">Nome</label> como?
  145. <form action="/reports" class="new_report" id="new_report" method="post"> <label for="report_name">Nome</label> <input id="report_name" size="30"

    name="report[name]" type="text" /> <input name="commit" type="submit" value="Criar Relatório" /> </form> <label for="report_name">Nome</label> como?
  146. <form action="/reports" class="new_report" id="new_report" method="post"> <label for="report_name">Nome</label> <input id="report_name" size="30"

    name="report[name]" type="text" /> <input name="commit" type="submit" value="Criar Relatório" /> </form> <label for="report_name">Nome</label> como?
  147. <form action="/reports" class="new_report" id="new_report" method="post"> <label for="report_name">Nome</label> <input id="report_name" size="30"

    name="report[name]" type="text" /> <input name="commit" type="submit" value="Criar Relatório" /> </form> <input id="report_name" size="30" name="report[name]" type="text" /> como?
  148. <form action="/reports" class="new_report" id="new_report" method="post"> <label for="report_name">Nome</label> <input id="report_name" size="30"

    name="report[name]" type="text" /> <input name="commit" type="submit" value="Criar Relatório" /> </form> <input id="report_name" size="30" name="report[name]" type="text" /> como?
  149. <form action="/reports" class="new_report" id="new_report" method="post"> <label for="report_name">Nome</label> <input id="report_name" size="30"

    name="report[name]" type="text" /> <input name="commit" type="submit" value="Criar Relatório" /> </form> <input id="report_name" size="30" name="report[name]" type="text" /> como?
  150. <form action="/reports" class="new_report" id="new_report" method="post"> <label for="report_name">Nome</label> <input id="report_name" size="30"

    name="report[name]" type="text" /> <input name="commit" type="submit" value="Criar Relatório" /> </form> <input name="commit" type="submit" value="Criar Relatório" /> como?
  151. <form action="/reports" class="new_report" id="new_report" method="post"> <label for="report_name">Nome</label> <input id="report_name" size="30"

    name="report[name]" type="text" /> <input name="commit" type="submit" value="Criar Relatório" /> </form> <input name="commit" type="submit" value="Criar Relatório" /> como?
  152. API Active Model

  153. ActiveModel ::Lint::Tests

  154. module ActiveModel::Lint::Tests def test_to_key; end def test_to_param; end def test_to_partial_path;

    end def test_valid?; end def test_persisted?; end def test_model_naming; end def test_errors_aref; end def test_errors_full_messages; end end
  155. module ActiveModel::Lint::Tests def test_to_key; end def test_to_param; end def test_to_partial_path;

    end def test_valid?; end def test_persisted?; end def test_model_naming; end def test_errors_aref; end def test_errors_full_messages; end end to_key to_param to_partial_path valid? persisted? model_naming errors_aref errors_full_messages
  156. module ActiveModel::Lint::Tests def test_to_key; end def test_to_param; end def test_to_partial_path;

    end def test_valid?; end def test_persisted?; end def test_model_naming; end def test_errors_aref; end def test_errors_full_messages; end end to_key to_param to_partial_path valid? persisted? model_naming errors_aref errors_full_messages Naming
  157. class Report extend ActiveModel::Naming end

  158. puts Report.model_name #=> "Report" puts Report.model_name.singular #=> report puts Report.model_name.plural

    #=> reports puts Report.model_name.param_key #=> report
  159. puts Report.model_name #=> "Report" puts Report.model_name.singular #=> report puts Report.model_name.plural

    #=> reports puts Report.model_name.param_key #=> report model_name "Report" model_name.singular report model_name.plural reports model_name.param_key report
  160. module ActiveModel::Lint::Tests def test_to_key; end def test_to_param; end def test_to_partial_path;

    end def test_valid?; end def test_persisted?; end def test_model_naming; end def test_errors_aref; end def test_errors_full_messages; end end to_key to_param to_partial_path valid? persisted? model_naming errors_aref errors_full_messages Conversion
  161. module ActiveModel::Lint::Tests def test_to_key; end def test_to_param; end def test_to_partial_path;

    end def test_valid?; end def test_persisted?; end def test_model_naming; end def test_errors_aref; end def test_errors_full_messages; end end to_key to_param to_partial_path valid? persisted? model_naming errors_aref errors_full_messages Implementar
  162. class Report include ActiveModel::Conversion attr_accessor :id def persisted? id.present? end

    end
  163. report = Report.new report.id = 1 puts report.persisted? #=> true

    puts report.to_key #=> [1] puts report.to_param #=> '1'
  164. report = Report.new report.id = 1 puts report.persisted? #=> true

    puts report.to_key #=> [1] puts report.to_param #=> '1' report.persisted? #=> true report.to_key #=> [1] report.to_param #=> '1'
  165. module ActiveModel::Lint::Tests def test_to_key; end def test_to_param; end def test_to_partial_path;

    end def test_valid?; end def test_persisted?; end def test_model_naming; end def test_errors_aref; end def test_errors_full_messages; end end to_key to_param to_partial_path valid? persisted? model_naming errors_aref errors_full_messages Validations
  166. require 'minitest/autorun' class OmglolTest < MiniTest::Unit::TestCase include ActiveModel::Lint::Tests def setup

    @model = Omglol.new end end
  167. require 'minitest/autorun' class OmglolTest < MiniTest::Unit::TestCase include ActiveModel::Lint::Tests def setup

    @model = Omglol.new end end include ActiveModel::Lint::Tests def setup @model = Omglol.new end
  168. require 'active_model' class Omglol extend ActiveModel::Naming include ActiveModel::Conversion include ActiveModel::Validations

    def persisted?; false; end end
  169. require 'active_model' class Omglol extend ActiveModel::Naming include ActiveModel::Conversion include ActiveModel::Validations

    def persisted?; false; end end extend ActiveModel::Naming include ActiveModel::Conversion include ActiveModel::Validations def persisted?; false; end
  170. $ ruby activemodel_test.rb Run options: --seed 9998 # Running tests:

    ........ Finished tests in 0.029155s, 274.3955 tests/s, 1303.3785 assertions/s. 8 tests, 38 assertions, 0 failures, 0 errors, 0 skips
  171. $ ruby activemodel_test.rb Run options: --seed 9998 # Running tests:

    ........ Finished tests in 0.029155s, 274.3955 tests/s, 1303.3785 assertions/s. 8 tests, 38 assertions, 0 failures, 0 errors, 0 skips
  172. Action Pack?

  173. form_for @report

  174. form_for @report do |f| f.label :name f.text_field :name f.submit end

  175. <form action="/reports" class="new_report" id="new_report" method="post"> <form action="/reports/1" class="edit_report" id="edit_report_1" method="post">

    new edit
  176. url_for @report

  177. url_for @report @report.persisted?

  178. url_for @report @report.persisted? @report.to_param

  179. url_for @report /reports /reports/1

  180. dom_class @report

  181. dom_class @report @report.class. model_name.param_key

  182. dom_class @report @report.class. model_name.param_key “report”

  183. dom_class @report, :new

  184. dom_class @report, :new “new_report”

  185. dom_id @report

  186. dom_id @report dom_class @report

  187. dom_id @report dom_class @report new_report / edit_report

  188. dom_id @report

  189. dom_id @report @report.to_key

  190. dom_id @report @report.to_key edit_report_1

  191. f.label :name

  192. f.label :name model_name.param_key

  193. f.label :name model_name.param_key “report_name”

  194. f.label :name

  195. f.label :name @report.class. human_attribute_name

  196. f.label :name @report.class. human_attribute_name “Nome”

  197. f.label :name <label for="report_name">Nome</label>

  198. f.text_field :name

  199. f.text_field :name model_name.param_key

  200. f.text_field :name model_name.param_key “report_name”

  201. f.text_field :name model_name.param_key “report_name” “report[name]”

  202. f.text_field :name <input id="report_name" size="30" name="report[name]" type="text" />

  203. f.submit

  204. f.submit @report.persisted?

  205. f.submit @report.persisted? “Criar” ou “Atualizar”

  206. f.submit

  207. f.submit model_name.human

  208. f.submit model_name.human “Relatório”

  209. f.submit <input name="commit" type="submit" value="Criar Relatório" />

  210. form_for @report

  211. form_for @report url_for dom_id dom_class

  212. form_for @report

  213. form_for @report model_name persisted? to_key

  214. link_to “...”, @report

  215. link_to “...”, @report url_for @report

  216. div_for @report

  217. div_for @report dom_id @report

  218. div_for @report dom_id @report dom_class @report

  219. render @report render @reports

  220. render @report

  221. render @report app/views/ reports/_report.html.erb

  222. render @report

  223. render @report @report.to_partial_path

  224. render @report @report.to_partial_path reports/_report

  225. respond_to :html, :json respond_with @report

  226. respond_to :html, :json

  227. respond_to :html, :json html? json?

  228. respond_with @report

  229. respond_with @report success? failure?

  230. respond_with @report

  231. respond_with @report @report.errors.empty?

  232. module ActiveModel::Lint::Tests def test_to_key; end def test_to_param; end def test_to_partial_path;

    end def test_valid?; end def test_persisted?; end def test_model_naming; end def test_errors_aref; end def test_errors_full_messages; end end to_key to_param to_partial_path valid? persisted? model_naming errors_aref errors_full_messages
  233. Validations?

  234. module ActiveModel::Lint::Tests def test_to_key; end def test_to_param; end def test_to_partial_path;

    end def test_valid?; end def test_persisted?; end def test_model_naming; end def test_errors_aref; end def test_errors_full_messages; end end to_key to_param to_partial_path valid? persisted? model_naming errors_aref errors_full_messages Validations?
  235. Rails 4 SPOILER

  236. module ActiveModel::Lint::Tests def test_to_key; end def test_to_param; end def test_to_partial_path;

    end def test_valid?; end def test_persisted?; end def test_model_naming; end def test_errors_aref; end def test_errors_full_messages; end end RAILS 4 to_key to_param to_partial_path valid? persisted? model_naming errors_aref errors_full_messages
  237. require 'minitest/autorun' class OmglolTest < MiniTest::Unit::TestCase include ActiveModel::Lint::Tests def setup

    @model = Omglol.new end end RAILS 4
  238. require 'minitest/autorun' class OmglolTest < MiniTest::Unit::TestCase include ActiveModel::Lint::Tests def setup

    @model = Omglol.new end end RAILS 4 include ActiveModel::Lint::Tests def setup @model = Omglol.new end
  239. require 'active_model' class Omglol extend ActiveModel::Naming include ActiveModel::Conversion def persisted?;

    false; end def errors; Hash.new([]); end end RAILS 4
  240. require 'active_model' class Omglol extend ActiveModel::Naming include ActiveModel::Conversion def persisted?;

    false; end def errors; Hash.new([]); end end RAILS 4 def errors; Hash.new([]); end
  241. require 'active_model' class Omglol extend ActiveModel::Naming include ActiveModel::Conversion def persisted?;

    false; end def errors; Hash.new([]); end end RAILS 4 extend ActiveModel::Naming include ActiveModel::Conversion def persisted?; false; end def errors; Hash.new([]); end def errors; Hash.new([]); end
  242. $ ruby activemodel_master_test.rb Run options: --seed 10850 # Running tests:

    ...... Finished tests in 0.002590s, 2316.6023 tests/s, 11583.0116 assertions/s. 6 tests, 30 assertions, 0 failures, 0 errors, 0 skip
  243. $ ruby activemodel_master_test.rb Run options: --seed 10850 # Running tests:

    ...... Finished tests in 0.002590s, 2316.6023 tests/s, 11583.0116 assertions/s. 6 tests, 30 assertions, 0 failures, 0 errors, 0 skip
  244. API !

  245. Active Model <3 <3 <3

  246. Review

  247. DRY Active Model

  248. API Active Model

  249. Bônus Track!

  250. Rails 4 SPOILER

  251. require 'active_model' class Omglol extend ActiveModel::Naming include ActiveModel::Conversion def persisted?;

    false; end def errors; Hash.new([]); end end RAILS 4
  252. require 'active_model' class Omglol extend ActiveModel::Naming include ActiveModel::Conversion def persisted?;

    false; end def errors; Hash.new([]); end end RAILS 4 extend ActiveModel::Naming include ActiveModel::Conversion def persisted?; false; end def errors; Hash.new([]); end
  253. ActiveModel ::Model

  254. module Model def self.included(base) #:nodoc: base.class_eval do extend ActiveModel::Naming extend

    ActiveModel::Translation include ActiveModel::Validations include ActiveModel::Conversion end end def initialize(params={}) params.each do |attr, value| self.public_send("#{attr}=", value) end if params end def persisted? false end end RAILS 4
  255. module Model def self.included(base) #:nodoc: base.class_eval do extend ActiveModel::Naming extend

    ActiveModel::Translation include ActiveModel::Validations include ActiveModel::Conversion end end def initialize(params={}) params.each do |attr, value| self.public_send("#{attr}=", value) end if params end def persisted? false end end RAILS 4 extend ActiveModel::Naming extend ActiveModel::Translation include ActiveModel::Validations include ActiveModel::Conversion def persisted? false end
  256. require 'active_model' class Omglol include ActiveModel::Model end RAILS 4

  257. require 'active_model' class Omglol include ActiveModel::Model end RAILS 4 include

    ActiveModel::Model
  258. form_for @omglol

  259. render @omglol

  260. respond_with @omglol

  261. Active Model <3 <3 <3

  262. None
  263. Active Model Attribute Methods Callbacks Dirty Mass Assignment Secure Password

    Observing Serialization Validations Validator Model Conversion Naming Translation Lint
  264. plataformatec/ mail_form

  265. http://pragprog.com/book/jvrails/crafting-rails-applications

  266. WITH GREAT MOUSTACHE COMES GREAT RESPONSIBILITY

  267. Estamos contratando! plataformatec.com.br

  268. None
  269. APLICAÇÕES MELHORES ACTIVE MODEL @cantoniodasilva Obrigado!