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

The Active Model

The Active Model

A brief introduction with examples to Rails Active Model library.

Avatar for Luis Alfredo Lorenzo

Luis Alfredo Lorenzo

August 25, 2016

More Decks by Luis Alfredo Lorenzo

Other Decks in Technology

Transcript

  1. THE ACTIVE MODEL THE ACTIVE MODEL THE ACTIVE MODEL THE

    ACTIVE MODEL THE ACTIvE MODEL THE ACTIVE MODEL_ POR: LUIS ALFREDO LORENZO A.K.A BABASBOT
  2. class Pokemon extend ActiveModel::Callbacks attr_accessor :level, :name define_model_callbacks :level_up around_level_up

    :evolve def level_up run_callbacks(:level_up) do self.level += 1 end end def evolve puts 'What? #{name} is evolving!' yield puts ‘#{name} has evolved into Raichu.' end end
  3. class Pokemon include ActiveModel::Dirty define_attribute_methods :level def level @level end

    def level=(val) level_will_change! unless val == @level @level = val end def save changes_applied end end