Slide 1

Slide 1 text

The Elusive Attribute Chris Salzberg

Slide 2

Slide 2 text

As we know, there are known knowns; there are things we know we know. We also know there are known unknowns; that is to say we know there are some things we do not know. But there are also unknown unknowns - the ones we don't know we don't know. - Donald Rumsfeld United States Secretary of Defense (2003)

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

People who are really into bikes [...] start out from the frame. They represent that small percentage of people who get the drawing perfectly right. Most other people tend to start from what they are most sure of, so for nearly everyone it’s the wheels. [...] I saw many people drawing all the rest in the hope that positioning handlebar, saddle and pedals would help them figure out the frame. - Gianluca Gimini

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

@attributes Name: Chris Salzberg Handle: shioyama Born: Montreal, CA Based: Tokyo, JP Company: Degica Blog: dejimata.com Tags: Module Builder, Mobility

Slide 15

Slide 15 text

1. methods & columns

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

class Post < ApplicationRecord end < 6.0

Slide 18

Slide 18 text

class Post < ApplicationRecord end Post.ancestors => [Post, Post::GeneratedAssociationMethods, #, ApplicationRecord(abstract), ApplicationRecord::GeneratedAssociationMethods, #, AR::Base, ... ] < 6.0

Slide 19

Slide 19 text

class Post < ApplicationRecord end Post.ancestors => [Post, Post::GeneratedAssociationMethods, #, ApplicationRecord(abstract), ApplicationRecord::GeneratedAssociationMethods, #, AR::Base, ... ] < 6.0

Slide 20

Slide 20 text

class Post < ApplicationRecord end Post.ancestors => [Post, Post::GeneratedAssociationMethods, Post::GeneratedAttributeMethods, ApplicationRecord(abstract), ApplicationRecord::GeneratedAssociationMethods, ApplicationRecord::GeneratedAttributeMethods, ActiveRecord::Base, ... ] >= 6.0

Slide 21

Slide 21 text

post = Post.new(title: "The Elusive Attribute")

Slide 22

Slide 22 text

post = Post.new(title: "The Elusive Attribute") post.method(:title) => #

Slide 23

Slide 23 text

post = Post.new(title: "The Elusive Attribute") post.method(:title).owner => Post::GeneratedAttributeMethods

Slide 24

Slide 24 text

=> [ ... :title_before_type_cast, :title_came_from_user?, :title?, :title_changed?, :title_change, :title_will_change!, :title_was, :title_previously_changed?, ... ] post = Post.new(title: "The Elusive Attribute") post.method(:title).owner.instance_methods Attribute Methods

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

post = Post.new(title: "The Elusive Attribute") mod = post.method(:title).owner

Slide 28

Slide 28 text

post = Post.new(title: "The Elusive Attribute") mod = post.method(:title).owner mod.instance_methods.each do |meth| mod.send(:remove_method, meth) end

Slide 29

Slide 29 text

post = Post.new(title: "The Elusive Attribute") mod = post.method(:title).owner mod.instance_methods.each do |meth| mod.send(:remove_method, meth) end mod.instance_methods #=> []

Slide 30

Slide 30 text

post = Post.new(title: "The Elusive Attribute") mod = post.method(:title).owner mod.instance_methods.each do |meth| mod.send(:remove_method, meth) end mod.instance_methods post.method(:title) # raises NameError: undefined method `title'

Slide 31

Slide 31 text

post = Post.new(title: "The Elusive Attribute") mod = post.method(:title).owner mod.instance_methods.each do |meth| mod.send(:remove_method, meth) end mod.instance_methods post.method(:title) post.title #=> ...

Slide 32

Slide 32 text

post = Post.new(title: "The Elusive Attribute") mod = post.method(:title).owner mod.instance_methods.each do |meth| mod.send(:remove_method, meth) end mod.instance_methods post.method(:title) post.title #=> "The Elusive Attribute"

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

cache = AR::Base.connection.schema_cache

Slide 36

Slide 36 text

cache = AR::Base.connection.schema_cache columns_hash = cache.instance_variable_get(:@columns_hash) #=> {}

Slide 37

Slide 37 text

cache = AR::Base.connection.schema_cache columns_hash = cache.instance_variable_get(:@columns_hash) columns_hash["posts"] = {}

Slide 38

Slide 38 text

cache = AR::Base.connection.schema_cache columns_hash = cache.instance_variable_get(:@columns_hash) columns_hash["posts"] = {} Post #=> Post()

Slide 39

Slide 39 text

cache = AR::Base.connection.schema_cache columns_hash = cache.instance_variable_get(:@columns_hash) columns_hash["posts"] = {} post = Post.new #=> #

Slide 40

Slide 40 text

cache = AR::Base.connection.schema_cache columns_hash = cache.instance_variable_get(:@columns_hash) columns_hash["posts"] = {} post = Post.new post.method(:title=) # raises NameError: undefined method `title='

Slide 41

Slide 41 text

cache = AR::Base.connection.schema_cache columns_hash = cache.instance_variable_get(:@columns_hash) columns_hash["posts"] = {} post = Post.new post.method(:title=) post.title = "The Elusive Attribute" # raises ActiveModel::UnknownAttributeError

Slide 42

Slide 42 text

cache = AR::Base.connection.schema_cache columns_hash = cache.instance_variable_get(:@columns_hash) columns_hash["posts"] = {} post = Post.new post.method(:title=) post.title = "The Elusive Attribute" post.attributes #=> {}

Slide 43

Slide 43 text

cache = AR::Base.connection.schema_cache columns_hash = cache.instance_variable_get(:@columns_hash) columns_hash["posts"] = {} post = Post.first # SELECT "posts".* FROM "posts" ... #=> #

Slide 44

Slide 44 text

cache = AR::Base.connection.schema_cache columns_hash = cache.instance_variable_get(:@columns_hash) columns_hash["posts"] = {} post = Post.first post.method(:title) # raises NameError: undefined method `title'

Slide 45

Slide 45 text

cache = AR::Base.connection.schema_cache columns_hash = cache.instance_variable_get(:@columns_hash) columns_hash["posts"] = {} post = Post.first post.method(:title) post.title #=> ?

Slide 46

Slide 46 text

cache = AR::Base.connection.schema_cache columns_hash = cache.instance_variable_get(:@columns_hash) columns_hash["posts"] = {} post = Post.first post.method(:title) post.title #=> "The Elusive Attribute"

Slide 47

Slide 47 text

cache = AR::Base.connection.schema_cache columns_hash = cache.instance_variable_get(:@columns_hash) columns_hash["posts"] = {} post = Post.first post.method(:title) post.title post.attributes #=> {"id"=>1, "title"=>"The Elusive Attribute", # "created_at"=>2019-03-13 05:09:10 UTC, # "updated_at"=>2019-03-29 07:57:54 UTC}

Slide 48

Slide 48 text

result = AR::Base.connection.exec_query( 'SELECT posts.* FROM posts') #=> #

Slide 49

Slide 49 text

result = AR::Base.connection.exec_query( 'SELECT posts.* FROM posts') #=> # result.columns #=> ["id", "title", "created_at", "updated_at"] result.rows #=> [[1, "The Elusive Attribute", "2019-03-15...

Slide 50

Slide 50 text

result = AR::Base.connection.exec_query( 'SELECT posts.* FROM posts') #=> # result.columns #=> ["id", "title", "created_at", "updated_at"] result.rows #=> [[1, "The Elusive Attribute", "2019-03-15... Post.column_names #=> []

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

Post.select('title as foo')

Slide 56

Slide 56 text

post = Post.select('title as foo').first post.foo #=> "The Elusive Attribute"

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

2. Matchers & Dispatchers

Slide 59

Slide 59 text

Constraints

Slide 60

Slide 60 text

Constraints 1. Methods are fast, method missing is slow

Slide 61

Slide 61 text

Constraints 1. Methods are fast, method missing is slow 2. schema is static, attributes are dynamic

Slide 62

Slide 62 text

Constraints 1. Methods are fast, method missing is slow 2. schema is static, attributes are dynamic “front door” “back door”

Slide 63

Slide 63 text

prefix: "" suffix: "_was" Matcher (Attribute Method)

Slide 64

Slide 64 text

prefix: "" suffix: "_was" "title" attr_name

Slide 65

Slide 65 text

prefix: "" suffix: "_was" "title_was" "title" method_name

Slide 66

Slide 66 text

prefix: "" suffix: "_was" "title_was" "attribute_was" "title" target

Slide 67

Slide 67 text

prefix: "" suffix: "_was" def title_was(*args) attribute_was("title", *args) end "title" Dispatcher

Slide 68

Slide 68 text

“Front Door” attribute_method_matchers.each do |matcher| method_name = matcher.method_name(attr_name) define_proxy_call true, generated_attribute_methods, method_name, matcher.target, attr_name.to_s end

Slide 69

Slide 69 text

“Front Door” attribute_method_matchers.each do |matcher| method_name = matcher.method_name(attr_name) define_proxy_call true, generated_attribute_methods, method_name, matcher.target, attr_name.to_s end “title”

Slide 70

Slide 70 text

“Front Door” attribute_method_matchers.each do |matcher| method_name = matcher.method_name(attr_name) define_proxy_call true, generated_attribute_methods, method_name, matcher.target, attr_name.to_s end “title_was” “attribute_was” “title”

Slide 71

Slide 71 text

prefix: "" suffix: "_was"

Slide 72

Slide 72 text

regex: /^(.*)_was$/

Slide 73

Slide 73 text

"foo_was" regex: /^(.*)_was$/ method_name

Slide 74

Slide 74 text

"foo" regex: /^(.*)_was$/ match("foo_was") attr_name

Slide 75

Slide 75 text

"foo", "attribute_was" regex: /^(.*)_was$/ match("foo_was") target

Slide 76

Slide 76 text

“Back Door” def attribute_method_matchers_matching(method_name) matchers = attribute_method_matchers. partition(&:plain?).reverse.flatten(1) matchers.map { |matcher| matcher.match(method_name) }.compact end

Slide 77

Slide 77 text

“Back Door” def attribute_method_matchers_matching(method_name) matchers = attribute_method_matchers. partition(&:plain?).reverse.flatten(1) matchers.map { |matcher| matcher.match(method_name) }.compact end (See PR #36005)

Slide 78

Slide 78 text

“Back Door” def attribute_method_matchers_matching(method_name) matchers = attribute_method_matchers. partition(&:plain?).reverse.flatten(1) matchers.map { |matcher| matcher.match(method_name) }.compact end “foo_was”

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

module ActiveModel::Dirty extend ActiveSupport::Concern include ActiveModel::AttributeMethods included do attribute_method_suffix "_changed?", "_change", "_will_change!", "_was" # Dispatch target for *_was attribute methods. def attribute_was(attr) attribute_changed?(attr) ? changed_attributes[attr] : _read_attribute(attr) end

Slide 88

Slide 88 text

module ActiveModel::Dirty extend ActiveSupport::Concern include ActiveModel::AttributeMethods included do attribute_method_suffix "_changed?", "_change", "_will_change!", "_was" # Dispatch target for *_was attribute methods. def attribute_was(attr) attribute_changed?(attr) ? changed_attributes[attr] : _read_attribute(attr) end

Slide 89

Slide 89 text

module ActiveModel::AttributeMethods included do class_attribute :attribute_method_matchers, instance_writer: false, default: [ ClassMethods::AttributeMethodMatcher.new ] # ... end end

Slide 90

Slide 90 text

module ActiveModel::AttributeMethods included do class_attribute :attribute_method_matchers, instance_writer: false, default: [ ClassMethods::AttributeMethodMatcher.new ] # ... end end

Slide 91

Slide 91 text

module ActiveModel::AttributeMethods included do class_attribute :attribute_method_matchers, instance_writer: false, default: [ ClassMethods::AttributeMethodMatcher.new ] # ... end end def initialize(options = {}) @prefix = options.fetch(:prefix, "") @suffix = options.fetch(:suffix, "")

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

module ActiveRecord module AttributeMethods module Read # ... def _read_attribute(attr_name, &b) @attributes.fetch_value(attr_name.to_s, &b) end alias :attribute :_read_attribute private :attribute end end end

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

No content

Slide 97

Slide 97 text

3. A Better Frame

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

post = Post.select('title as foo').first post.foo #=> "The Elusive Attribute"

Slide 100

Slide 100 text

post = Post.select('title as foo').first post.foo #=> "The Elusive Attribute" post.foo = "This cannot be saved"

Slide 101

Slide 101 text

post = Post.select('title as foo').first post.foo #=> "The Elusive Attribute" post.foo = "This cannot be saved" post.foo_was

Slide 102

Slide 102 text

post = Post.select('title as foo').first post.foo #=> "The Elusive Attribute" post.foo = "This cannot be saved" post.foo_was post.foo_change

Slide 103

Slide 103 text

post = Post.select('1 as one').first post.one #=> 1 post.one = 2 post.one_was post.one_change

Slide 104

Slide 104 text

post = Post.select('1 as one').first post.one #=> 1 post.one = 2 # post.one #=> 2 post.one_was #=> 1 post.one_change #=> [1, 2]

Slide 105

Slide 105 text

payments = Payment.select( 'amount, amount*0.08 as tax, amount*0.02 as tax_change')

Slide 106

Slide 106 text

payments = Payment.select( 'amount, amount*0.08 as tax, amount*0.02 as tax_change') payment = payments.first payment.amount #=> 100 payment.tax #=> 8 payment.tax_change #=> ?

Slide 107

Slide 107 text

payments = Payment.select( 'amount, amount*0.08 as tax, amount*0.02 as tax_change') payment = payments.first payment.amount #=> 100 payment.tax #=> 8 payment.tax_change #=> nil

Slide 108

Slide 108 text

"tax", "attribute_change" regex: /^(.*)_change$/ match("tax_change")

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

DON’T JUST UNPACK IT,

Slide 111

Slide 111 text

DON’T JUST UNPACK IT, FIX IT

Slide 112

Slide 112 text

Chris Salzberg @shioyama /dejimata.com