Class: AirctiveRecord::Base
Class Method Summary
collapse
Instance Method Summary
collapse
#attribute_present?, #attributes, #attributes=, #read_attribute, #write_attribute
Methods included from Callbacks
#destroy
#valid?
Constructor Details
#initialize(attributes = {}, **kwargs) ⇒ Base
Returns a new instance of Base.
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/airctiverecord/base.rb', line 33
def initialize(attributes = {}, **kwargs)
id = kwargs.delete(:id)
created_at = kwargs.delete(:created_at)
attr_hash = attributes.respond_to?(:to_h) ? attributes.to_h : {}
all_attrs = attr_hash.merge(kwargs)
mapped_attrs = {}
all_attrs.each do |key, value|
field_name = self.class.field_mappings[key.to_s] || key.to_s
mapped_attrs[field_name.to_s] = value end
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.0.0")
if mapped_attrs.empty?
super(id: id, created_at: created_at)
else
super(mapped_attrs, id: id, created_at: created_at)
end
else
super(mapped_attrs, id: id, created_at: created_at)
end
clear_changes_information
end
|
Class Method Details
.attribute(name, airtable_field_name = nil, **options) ⇒ Object
20
21
22
23
|
# File 'lib/airctiverecord/base.rb', line 20
def attribute(name, airtable_field_name = nil, **options)
column_names << name.to_s unless column_names.include?(name.to_s)
field(name, airtable_field_name, **options)
end
|
.column_names ⇒ Object
16
17
18
|
# File 'lib/airctiverecord/base.rb', line 16
def column_names
@column_names ||= []
end
|
.relation_class ⇒ Object
each model gets its own Relation class
26
27
28
|
# File 'lib/airctiverecord/base.rb', line 26
def relation_class
@relation_class ||= Class.new(AirctiveRecord::Relation)
end
|
.relation_class_name ⇒ Object
30
|
# File 'lib/airctiverecord/base.rb', line 30
def relation_class_name = "#{name}::Relation"
|
Instance Method Details
#assign_attributes(attrs) ⇒ Object
65
66
67
|
# File 'lib/airctiverecord/base.rb', line 65
def assign_attributes(attrs)
self.attributes = attrs
end
|
#persisted? ⇒ Boolean
118
|
# File 'lib/airctiverecord/base.rb', line 118
def persisted? = !new_record?
|
#reload ⇒ Object
108
109
110
111
112
113
114
115
116
|
# File 'lib/airctiverecord/base.rb', line 108
def reload
return self if new_record?
reloaded = self.class.find(id)
@fields = reloaded.fields
@created_at = reloaded.created_at
clear_changes_information
self
end
|
#save(**options) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/airctiverecord/base.rb', line 74
def save(**options)
return false unless valid?(new_record? ? :create : :update)
run_callbacks :save do
if new_record?
run_callbacks :create do
super(**options)
end
else
run_callbacks :update do
super(**options)
end
end
end
changes_applied
true
end
|
#save!(**options) ⇒ Object
92
93
94
95
96
|
# File 'lib/airctiverecord/base.rb', line 92
def save!(**options)
raise RecordInvalid, errors.full_messages.join(", ") unless valid?(new_record? ? :create : :update)
save(**options)
end
|
#serializable_fields ⇒ Object
69
70
71
72
|
# File 'lib/airctiverecord/base.rb', line 69
def serializable_fields
fields.reject { |k, _v| self.class.readonly_fields.include?(k) }
end
|
#to_key ⇒ Object
122
|
# File 'lib/airctiverecord/base.rb', line 122
def to_key = persisted? ? [id] : nil
|
#to_model ⇒ Object
124
|
# File 'lib/airctiverecord/base.rb', line 124
def to_model = self
|
#to_param ⇒ Object
120
|
# File 'lib/airctiverecord/base.rb', line 120
def to_param = id
|
#update(attributes) ⇒ Object
98
99
100
101
|
# File 'lib/airctiverecord/base.rb', line 98
def update(attributes)
attributes.each { |key, value| send("#{key}=", value) }
save
end
|
#update!(attributes) ⇒ Object
103
104
105
106
|
# File 'lib/airctiverecord/base.rb', line 103
def update!(attributes)
attributes.each { |key, value| send("#{key}=", value) }
save!
end
|