bsm-models 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,87 @@
1
+ module Bsm::Model::HasManySerialized
2
+ extend ActiveSupport::Concern
3
+
4
+ module ClassMethods
5
+
6
+ def has_many_serialized(name, options = {})
7
+ Builder.build(self, name.to_sym, options)
8
+ end
9
+
10
+ end
11
+
12
+ class Coder
13
+
14
+ def dump(obj)
15
+ ActiveSupport::JSON.encode(obj)
16
+ end
17
+
18
+ def load(value)
19
+ return [] if value.nil?
20
+ return value unless value.is_a?(String)
21
+
22
+ obj = ActiveSupport::JSON.decode(value) || []
23
+ unless obj.is_a?(Array)
24
+ raise ActiveRecord::SerializationTypeMismatch, "Attribute was supposed to be an Array, but was a #{obj.class}"
25
+ end
26
+ obj
27
+ end
28
+
29
+ end
30
+
31
+ class Builder < ActiveRecord::Associations::Builder::Association
32
+
33
+ def build
34
+ validate_options
35
+ serialize_attribute
36
+ define_accessors
37
+ end
38
+
39
+ protected
40
+
41
+ def serialize_attribute
42
+ model.serialize attribute_name, Coder.new
43
+ end
44
+
45
+ def define_readers
46
+ name, attribute_name, klass = self.name, self.attribute_name, self.klass
47
+
48
+ model.redefine_method(name) do
49
+ klass.where(klass.primary_key => send(attribute_name))
50
+ end
51
+
52
+ model.redefine_method(attribute_name) do
53
+ read_attribute(attribute_name)
54
+ end
55
+ end
56
+
57
+ def define_writers
58
+ name, attribute_name, klass = self.name, self.attribute_name, self.klass
59
+
60
+ model.redefine_method("#{name}=") do |records|
61
+ records = Array.wrap(records)
62
+ records.each do |record|
63
+ next if record.is_a?(klass)
64
+ raise ActiveRecord::AssociationTypeMismatch, "#{klass.name} expected, got #{record.class}"
65
+ end
66
+ write_attribute attribute_name, records.map(&:id)
67
+ end
68
+
69
+ model.redefine_method("#{attribute_name}=") do |values|
70
+ send "#{name}=", klass.where(klass.primary_key => Array.wrap(values)).to_a
71
+ end
72
+ end
73
+
74
+ def attribute_name
75
+ @attribute_name ||= "#{name.to_s.singularize}_ids"
76
+ end
77
+
78
+ def klass
79
+ @klass ||= class_name.constantize
80
+ end
81
+
82
+ def class_name
83
+ @class_name ||= options[:class_name] || name.to_s.singularize.camelize
84
+ end
85
+
86
+ end
87
+ end
data/lib/bsm/model.rb CHANGED
@@ -7,6 +7,7 @@ module Bsm
7
7
  autoload :Deletable, 'bsm/model/deletable'
8
8
  autoload :EagerDescendants, 'bsm/model/eager_descendants'
9
9
  autoload :StiConvertable, 'bsm/model/sti_convertable'
10
+ autoload :HasManySerialized, 'bsm/model/has_many_serialized'
10
11
  end
11
12
 
12
13
  class Railtie < ::Rails::Railtie
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bsm-models
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-04 00:00:00.000000000Z
12
+ date: 2011-12-05 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: abstract
16
- requirement: &18772060 !ruby/object:Gem::Requirement
16
+ requirement: &12150960 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *18772060
24
+ version_requirements: *12150960
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activerecord
27
- requirement: &18768740 !ruby/object:Gem::Requirement
27
+ requirement: &12150340 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -35,10 +35,10 @@ dependencies:
35
35
  version: 3.2.0
36
36
  type: :runtime
37
37
  prerelease: false
38
- version_requirements: *18768740
38
+ version_requirements: *12150340
39
39
  - !ruby/object:Gem::Dependency
40
40
  name: railties
41
- requirement: &18745420 !ruby/object:Gem::Requirement
41
+ requirement: &12149560 !ruby/object:Gem::Requirement
42
42
  none: false
43
43
  requirements:
44
44
  - - ! '>='
@@ -49,7 +49,7 @@ dependencies:
49
49
  version: 3.2.0
50
50
  type: :runtime
51
51
  prerelease: false
52
- version_requirements: *18745420
52
+ version_requirements: *12149560
53
53
  description: ''
54
54
  email: dimitrij@blacksquaremedia.com
55
55
  executables: []
@@ -59,6 +59,7 @@ files:
59
59
  - lib/bsm/model/abstract.rb
60
60
  - lib/bsm/model/locale/en.yml
61
61
  - lib/bsm/model/deletable.rb
62
+ - lib/bsm/model/has_many_serialized.rb
62
63
  - lib/bsm/model/eager_descendants.rb
63
64
  - lib/bsm/model/sti_convertable.rb
64
65
  - lib/bsm/model/editable.rb