1 |
module Sprig |
14✔ |
2 |
module Seed |
14✔ |
3 |
class Record |
14✔ |
4 |
attr_reader :orm_record
|
14✔ |
5 |
|
|
6 |
delegate :errors, :to => :orm_record |
14✔ |
7 |
|
|
8 |
def self.new_or_existing(klass, attributes, find_params) |
14✔ |
9 |
orm_record = klass.where(find_params).first |
91✔ |
10 |
new(klass, attributes, orm_record) |
91✔ |
11 |
end
|
|
12 |
|
|
13 |
def initialize(klass, attributes, orm_record = nil) |
14✔ |
14 |
@klass = klass
|
603✔ |
15 |
@attributes = attributes
|
603✔ |
16 |
@orm_record = orm_record || klass.new
|
603✔ |
17 |
@existing = @orm_record.persisted? |
603✔ |
18 |
end
|
|
19 |
|
|
20 |
def save |
14✔ |
21 |
populate_attributes |
577✔ |
22 |
orm_record.save |
577✔ |
23 |
end
|
|
24 |
|
|
|
def save! |
14✔ |
|
populate_attributes |
× |
|
orm_record.save! |
× |
28 |
end
|
|
29 |
|
|
30 |
def existing? |
14✔ |
31 |
@existing
|
577✔ |
32 |
end
|
|
33 |
|
|
34 |
private |
14✔ |
35 |
|
|
36 |
attr_reader :attributes, :klass |
14✔ |
37 |
|
|
38 |
def populate_attributes |
14✔ |
39 |
attributes.each do |attribute|
|
577✔ |
40 |
orm_record.send(:"#{attribute.name}=", attribute.value)
|
1,180✔ |
41 |
end
|
|
42 |
end
|
|
43 |
end
|
|
44 |
end
|
|
45 |
end
|