active_record_content_blob 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/VERSION +1 -1
- data/active_record_content_blob.gemspec +1 -1
- data/lib/content_blob/blobable.rb +28 -14
- data/spec/spec_helper.rb +1 -0
- data/spec/unit/blobable_spec.rb +35 -7
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZjdmMjg5N2E0ZTA1ZDAzZWUxYTFhM2YxZTUzNjgzZjRlNjdlNWFiNA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NzA3YmRmMDQ1YWVlZjhlNDkwMGRlZTk1ZDE3MDlhZDE2OWUxMjk5Ng==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MjgwNTcwZDZhOGY4NzcxODY5NTAyZjk2MTRlN2ZiMjg2MWQ4MGY5MTRlZGUx
|
10
|
+
NjNkYjVjN2VmMGExMTc4OGQ5MDA3NGJjMDRjOTgyOGNiZTc4YzkyOWE5ZmU2
|
11
|
+
MTMzNzYwOTM5ZjE5YWIxMjc1MDM4ZGZlMjBhNmMxNGM5NThkOTQ=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZGYyYjFlYTZhNjg0NTA3MjQwMjcyNzBjMjFkZDUwYWFkNGU2NzAyMWUwNzRm
|
14
|
+
MDVlYWM4MGM5ZWEwNjg2MmUxYzAxN2ZiMTQzOGJjMGYxMDNlOTRkNWY0ODcx
|
15
|
+
NGJmMzNiN2UxMzgwNTA3NmY0YmM0MmMyODgxYTg5ZjRjNTJjNTE=
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.5
|
@@ -22,31 +22,44 @@ module ActiveRecordContentBlob
|
|
22
22
|
|
23
23
|
module ClassMethods
|
24
24
|
|
25
|
+
|
26
|
+
|
25
27
|
# returns a new record with an attached blob
|
26
|
-
|
28
|
+
# e.g. Record.build_with_a_blob(record_hsh, record_big_hsh)
|
29
|
+
def build_with_a_blob(hsh, blob_content=nil)
|
27
30
|
hsh_syms = hsh.symbolize_keys
|
28
|
-
|
29
|
-
#
|
30
|
-
|
31
|
+
|
32
|
+
# exclude :contents from instantiating a new Record
|
31
33
|
record = self.new(hsh_syms.reject{|k,v| k == :contents})
|
32
34
|
|
33
|
-
if blob_content.
|
34
|
-
#
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
end
|
35
|
+
stuff = if blob_content.present?
|
36
|
+
# build a blob using :blob_content
|
37
|
+
blob_content
|
38
|
+
elsif c = hsh_syms[:contents]
|
39
|
+
c
|
39
40
|
else
|
40
|
-
#
|
41
|
-
|
41
|
+
# then build a blob from the hsh
|
42
|
+
hsh
|
42
43
|
end
|
43
44
|
|
45
|
+
|
46
|
+
prepared_stuff = prepare_content_for_blob(stuff)
|
47
|
+
|
48
|
+
record.build_a_blob(prepared_stuff)
|
44
49
|
return record
|
45
50
|
end
|
46
51
|
|
47
|
-
|
48
|
-
|
52
|
+
|
53
|
+
|
54
|
+
def create_with_a_blob(hsh, blob_content=nil)
|
55
|
+
build_with_a_blob(hsh, blob_content).save
|
56
|
+
end
|
57
|
+
|
58
|
+
# allow this to be redefined
|
59
|
+
def prepare_content_for_blob(some_content)
|
60
|
+
some_content
|
49
61
|
end
|
62
|
+
|
50
63
|
end # classmethods
|
51
64
|
|
52
65
|
## instance methods
|
@@ -54,6 +67,7 @@ module ActiveRecordContentBlob
|
|
54
67
|
self.build_content_blob(contents: some_content)
|
55
68
|
end
|
56
69
|
|
70
|
+
|
57
71
|
|
58
72
|
def has_blob?
|
59
73
|
content_blob.exists?
|
data/spec/spec_helper.rb
CHANGED
data/spec/unit/blobable_spec.rb
CHANGED
@@ -46,30 +46,58 @@ describe "Blobable" do
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
-
describe 'class convenience method ::
|
49
|
+
describe 'class convenience method ::build_with_a_blob' do
|
50
50
|
context 'one argument' do
|
51
51
|
it 'should build associated content_blob with Hash :contents' do
|
52
|
-
@record = MusicRecord.
|
52
|
+
@record = MusicRecord.build_with_a_blob({name: 'X', contents: [1]})
|
53
53
|
expect(@record.contents).to eq [1]
|
54
54
|
end
|
55
55
|
|
56
|
-
it 'should build
|
57
|
-
@record = MusicRecord.
|
58
|
-
expect(@record.contents).to
|
56
|
+
it 'should build a content_blob, eq to first Hash, even without :contents' do
|
57
|
+
@record = MusicRecord.build_with_a_blob(name: 'X')
|
58
|
+
expect(@record.contents).to eq( {name: 'X'})
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
62
|
context 'second argument' do
|
63
63
|
it 'should use second argument as :contents for built blob' do
|
64
|
-
@record = MusicRecord.
|
64
|
+
@record = MusicRecord.build_with_a_blob({name: 'A'}, [99])
|
65
65
|
expect(@record.contents).to eq [99]
|
66
66
|
end
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
70
|
+
|
71
|
+
context 'allow .prepare_content_blob hook to be redefined' do
|
72
|
+
before(:each) do
|
73
|
+
class MusicRecord < ActiveRecord::Base
|
74
|
+
def self.prepare_content_for_blob(some_content)
|
75
|
+
some_content.is_a?(Hash) ? [some_content] : Array(some_content)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
MusicRecord
|
80
|
+
end
|
81
|
+
|
82
|
+
after(:each) do
|
83
|
+
class MusicRecord < ActiveRecord::Base
|
84
|
+
def self.prepare_content_for_blob(some_content)
|
85
|
+
some_content
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'should redefine .prepare_content_blob as specified' do
|
91
|
+
@record = MusicRecord.build_with_a_blob({name: 'A'})
|
92
|
+
expect(@record.contents).to eq [{name: 'A'}]
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
end
|
97
|
+
|
70
98
|
context 'scopes' do
|
71
99
|
before(:each) do
|
72
|
-
@r1 = MusicRecord.
|
100
|
+
@r1 = MusicRecord.create_with_a_blob(name: 'A', contents: ['Z'])
|
73
101
|
@r2 = MusicRecord.create(name: 'qqq')
|
74
102
|
end
|
75
103
|
|