Slide 1

Slide 1 text

Introduction to
 Active Support

Slide 2

Slide 2 text

Notations $ irb # Shell command > str = "Hello World" # Ruby code #=> Hello World # Return value

Slide 3

Slide 3 text

Notations Object.method <= Class method Object#method <= Instance method

Slide 4

Slide 4 text

What is Active Support?

Slide 5

Slide 5 text

"Utility classes and Ruby extensions from Rails" (From https://github.com/rails/rails/tree/master/activesupport)

Slide 6

Slide 6 text

Utility Classes • ActiveSupport::Callbacks • ActiveSupport::HashWithIndifferentAccess • ActiveSupport::MessageEncrypter • ActiveSupport::MessageVerifier • ... etc ...

Slide 7

Slide 7 text

Ruby Extensions • object.present? / object.blank? • date.yesterday / date.tomorrow • 1.day.ago / 2.months.from_now • ... etc ...

Slide 8

Slide 8 text

Part of Rails framework Available to be used inside Rails

Slide 9

Slide 9 text

# To use outside Rails $ gem install activesupport # in Ruby > require "activesupport/all"

Slide 10

Slide 10 text

Digging Deeper

Slide 11

Slide 11 text

Callbacks

Slide 12

Slide 12 text

Callbacks • Helpers to define and run callbacks • Use in Active Record, Action Pack, etc. • before_action, after_action, before_save, etc.

Slide 13

Slide 13 text

Callback Example class User < ActiveRecord::Base before_save :do_something def do_something # ... end end

Slide 14

Slide 14 text

Callback Example class Account include ActiveSupport::Callbacks define_callbacks :save set_callback :save, :before, :do_something def save run_callbacks :save do # ... end end def do_something # ... end end

Slide 15

Slide 15 text

MessageEncrypter Encrypts message with a key

Slide 16

Slide 16 text

MessageEncrypter > salt = SecureRandom.random_bytes(64) > key = ActiveSupport::KeyGenerator. new('password').generate_key(salt) > crypt = ActiveSupport::MessageEncryptor.new(key) > encrypted_data = crypt. encrypt_and_sign('my secret data') > crypt.decrypt_and_verify(encrypted_data)

Slide 17

Slide 17 text

Notifications

Slide 18

Slide 18 text

Notifications • Uses for logging purposes • Executer instrument an event that should be subscribed to: • Action View's "render" • Active Record's "execute SQL" • etc. • Listeners subscribe to those events from another part of the application

Slide 19

Slide 19 text

TimeZone

Slide 20

Slide 20 text

TimeZone • Contains full mapping of time zones • Perform time zone conversions

Slide 21

Slide 21 text

TimeZone > ActiveSupport::TimeZone.all > ActiveSupport::TimeZone.us_zones > Time.zone = "America/New_York" > time = Time.zone.now #=> "Fri, 20 Jun 2014 14:35:00 EDT -04:00" > time.in_time_zone("America/Los_Angeles") #=> "Fri, 20 Jun 2014 11:35:00 PDT -07:00"

Slide 22

Slide 22 text

Core Extensions

Slide 23

Slide 23 text

Array

Slide 24

Slide 24 text

Array#from Array#to

Slide 25

Slide 25 text

Array#from Array#to > array = [1, 2, 3, 4] > array.from(2) #=> [3, 4] > array.to(2) #=> [1,2,3]

Slide 26

Slide 26 text

Array#second Array#third Array#fourth Array#fifth Array#forty_two

Slide 27

Slide 27 text

Array Access > array = (1..100).to_a > array.first #=> 1 > array.second #=> 2 > array.third #=> 3 > array.fourth #=> 4 > array.fifth #=> 5 > array.forty_two #=> 42

Slide 28

Slide 28 text

Array#to_sentence

Slide 29

Slide 29 text

Array#to_sentence > fruits = %w(banana strawberry kiwi) > fruits.to_sentence #=> "banana, strawberry, and kiwi > sports = %w(football baseball) > sports.to_sentence #=> "football and baseball"

Slide 30

Slide 30 text

Array#in_groups_of Array#in_groups

Slide 31

Slide 31 text

Array#in_group_of > array = (1..10).to_a > array.in_group_of(3) # => [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, nil, nil]]

Slide 32

Slide 32 text

Array#in_groups > array = (1..10).to_a > array.in_groups(3) #=> [[1, 2, 3, 4], [5, 6, 7, nil], [8, 9, 10, nil]]

Slide 33

Slide 33 text

Date, Time, DateTime

Slide 34

Slide 34 text

Date.current Date.yesterday Date.tomorrow

Slide 35

Slide 35 text

Time#beginning_of_day Time#middle_of_day Time#end_of_day

Slide 36

Slide 36 text

Time#beginning_of_hour Time#end_of_hour

Slide 37

Slide 37 text

Time#all_day Date#all_week Date#all_month Date#all_year

Slide 38

Slide 38 text

Time#today? Time#past? Time#future?

Slide 39

Slide 39 text

Hash

Slide 40

Slide 40 text

Hash#deep_merge

Slide 41

Slide 41 text

Hash#deep_merge > h1 = { a: true, b: { c: [1, 2, 3] } } > h2 = { a: false, b: { x: [3, 4, 5] } } > h1.deep_merge(h2) #=> { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } }

Slide 42

Slide 42 text

Hash#except

Slide 43

Slide 43 text

Hash#except > hash = { one: 1, two: 2 } > hash.except(:one) #=> { two: 2 }

Slide 44

Slide 44 text

Hash#with_indifferent_access

Slide 45

Slide 45 text

# In Controller > params[:id] #=> 1 > params['id'] #=> 1 > params.class #=> ActiveSupport::HashWithIndifferentAccess

Slide 46

Slide 46 text

Hash#with_indifferent_access > hash = { one: 1 }.with_indifferent_access > hash[:one] #=> 1 > hash['one'] #=> 1

Slide 47

Slide 47 text

Hash#stringify_keys Hash#symbolize_keys

Slide 48

Slide 48 text

Hash#stringify_keys Hash#symbolize_keys > hash = { one: 1, 'two' => 2 } > hash.stringify_keys #=> { 'one' => 1, 'two' => 2 } > hash.symbolize_keys #=> { one: 1, two: 2 }

Slide 49

Slide 49 text

Hash#reverse_merge

Slide 50

Slide 50 text

Hash#reverse_merge > h1 = { one: 'one' } > h2 = { one: 'uno', two: 'dos' } > h1.merge(h2) #=> { one: 'uno', two: 'dos' } > h1.reverse_merge(h2) #=> { one: 'one', two: 'dos' }

Slide 51

Slide 51 text

Hash#slice

Slide 52

Slide 52 text

Hash#slice > hash = { one: 1, two: 2, three: 3 } > hash.slice(:one, :two) #=> { one: 1, two: 2 }

Slide 53

Slide 53 text

Integer

Slide 54

Slide 54 text

Integer#ordinalize

Slide 55

Slide 55 text

Integer#ordinalize > 1.ordinalize #=> "1st" > 2.ordinalize #=> "2nd"

Slide 56

Slide 56 text

Integer#ordinal

Slide 57

Slide 57 text

Integer#ordinalize > 1.ordinal #=> "st" > 2.ordinalize #=> "nd"

Slide 58

Slide 58 text

Integer#days Integer#months Integers#years

Slide 59

Slide 59 text

Examples > 1.month.ago > 1.month.from_now > 1.month.since(time) > 1.year.from(time)

Slide 60

Slide 60 text

Object

Slide 61

Slide 61 text

Object#present? Object#blank?

Slide 62

Slide 62 text

Object#try

Slide 63

Slide 63 text

Object#try > user = nil > user.name #=> NoMethodError > user.try(:name) #=> nil

Slide 64

Slide 64 text

Object#presence

Slide 65

Slide 65 text

Object#presence > name = "John" > puts "Hello #{name}" #=> "Hello John"

Slide 66

Slide 66 text

Object#presence > name = "" > puts "Hello #{name}" #=> "Hello "

Slide 67

Slide 67 text

Object#presence > name = "" > puts "Hello #{name.present? ? name : "Guest"}" #=> "Hello Guest"

Slide 68

Slide 68 text

Object#presence > name = "" > puts "Hello #{name.presence || "Guest"}" #=> "Hello Guest"

Slide 69

Slide 69 text

String

Slide 70

Slide 70 text

String#to_time String#to_date String#to_datetime

Slide 71

Slide 71 text

String#to_time String#to_date String#to_datetime > "13-12-2012".to_time #=> 2012-12-13 00:00:00 -0500 > "1-1-2012".to_date #=> Sun, 01 Jan 2012 > "1-1-2012".to_datetime #=> Sun, 01 Jan 2012 00:00:00 +0000

Slide 72

Slide 72 text

String#truncate

Slide 73

Slide 73 text

String#truncate > "Hello World".truncate(8) #=> "Hello Wo..."

Slide 74

Slide 74 text

String#singularize String#pluralize String#camelize String#titleize String#humanize

Slide 75

Slide 75 text

Inflections Example > "man".pluralize #=> "men" > "octopi".singularize #=> "octopus" > "user_name".camelize #=> "UserName" > "hello world".titlize #=> "Hello World" > "full_name".humanize #=> "Full name"

Slide 76

Slide 76 text

String#inquiry

Slide 77

Slide 77 text

String#inquiry > color = "red".inquiry > color.red? #=> true > color.blue? #=> false > Rails.env.development?

Slide 78

Slide 78 text

http://guides.rubyonrails.org/active_support_core_extensions.html