serialize_has_many 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MTRhYzllMTI1ODQ4NWY0MjcyOTBiNDFhZjMyMmNjMWU0ZTA3YTUwMQ==
4
+ NWI0NzAyYmIyM2IxMDdmODU3YzFhY2I2NGVkM2NiNzg0ZTIxNjE2NQ==
5
5
  data.tar.gz: !binary |-
6
- MTlmYzU2ZjkzM2Q0Y2FlYThiMjA0NjM2YWU1MTllNjA3YTk5NTlmOA==
6
+ NTllMTVmMjU1ZGZiYzVjOTI5YzkzZGRhNjMzMTZmZDMzZDgyOWVmYg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZmM4ODRlYzEzZTAzNWU0NjU4OWQzNmE4MTZkNzRkNDExM2Q1ZDhmMzQ0ZjU5
10
- ZGFlYzYzMDNiNTVjNjQ2YTJiZjRkNzQ4MzYwM2EyZDNhNjMxZmQ2ZjhjYmNh
11
- NWRlMmVlMjVjYjdmMjgwNjAzMTU1MzUwMzIwNTYyOTIwNzkwZGY=
9
+ YmMwYjg2N2ViMWFmY2Q0YWUwYTU5NmZmMmU5NDdjYzcwYTNkMGM5MWEyNDRi
10
+ MGMxNzY4YWM3ZmU0YjVjMjNhNzYyZjU3NjZlZDVlZWM5Mzc2NmQ5OWQ3OGE3
11
+ MmY4NGVlMzBhOTgxZjBhNDQ1NzI0YjgxN2JlODk4NGQ3YjUyZGI=
12
12
  data.tar.gz: !binary |-
13
- ZGE5ZTExNjc5ZTM0MGFlMmIyNWYwNmFkZTUwMzNmNTA3YzYyNTVhYzNiMzM5
14
- NWI3NzVmNGFhN2I0MjRlZTViYWZjMzQ5MmRhZGZmZTg2YjliMjhjZGZiYjNj
15
- OGI4ZTE3YTRlYmY3MjU3MTA4ZjgyMGQ0NmYyMGVhODI1NGNmZmI=
13
+ OGVhNjAxOTJmZmY4NjBkNzRhMDJhNmRkMGY4Y2ExODRhMGY0MWEwODg4ZjVj
14
+ MTI1MDViNjUyNDEwYTM2YjNjMDY2MzVhNzY5MWM2OTVmM2IwMDZjZWYxOWJm
15
+ OWMyMjIzNzM4MjhhNmZmOGJjODVhN2UxMzkzYzQ0NTg3ZTI2NDk=
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # SerializeHasMany
2
2
 
3
- [![Build Status](https://travis-ci.org/rdsubhas/serialize_has_many.svg?branch=master)](https://travis-ci.org/rdsubhas/serialize_has_many)
3
+ [![Build Status](https://travis-ci.org/rdsubhas/serialize_has_many.svg?branch=master)](https://travis-ci.org/rdsubhas/serialize_has_many) [![Code Climate](https://codeclimate.com/github/rdsubhas/serialize_has_many/badges/gpa.svg)](https://codeclimate.com/github/rdsubhas/serialize_has_many) [![Coverage Status](https://img.shields.io/coveralls/rdsubhas/serialize_has_many.svg)](https://coveralls.io/r/rdsubhas/serialize_has_many)
4
4
 
5
5
  Serializes `has_many` relationships into a single column while still doing attributes, validations, callbacks, nested forms and fields_for. Easy NoSQL with ActiveRecord!
6
6
 
@@ -24,8 +24,10 @@ Or install it yourself as:
24
24
 
25
25
  Assume you have a Parent has-many Child relation. To use `serialize_has_many`:
26
26
 
27
- * Child should respond to `attributes` and `new(attributes)`
28
- * Parent should have a `text` column attribute to store the serialized data
27
+ * Child should respond to `attributes` and `new(attributes)`. To be clear:
28
+ * `child.attributes` should give a hash
29
+ * `Child.new(attributes)` should take that hash
30
+ * Parent should have an attribute to store the serialized data, preferably `text` datatype
29
31
  * Add `serialize_has_many` in the Parent
30
32
 
31
33
  ### Example
@@ -33,6 +35,7 @@ Assume you have a Parent has-many Child relation. To use `serialize_has_many`:
33
35
  (For a real scenario, check `example/app/models/`)
34
36
 
35
37
  ```ruby
38
+ # Make Child to use ActiveModel::Model instead of ActiveRecord
36
39
  class Child
37
40
  include ActiveModel::Model
38
41
  attr_accessor :name, :age, ...
@@ -43,15 +46,29 @@ class Child
43
46
  end
44
47
  end
45
48
 
49
+ # Convert Parent to use serialize_has_many instead of has_many
46
50
  class Parent < ActiveRecord::Base
47
51
  include SerializeHasMany::Concern
48
- serialize_has_many <name-of-column>, Child,
49
- using: <JSON|YAML>,
50
- validate: <true|false>,
51
- reject_if: <proc to reject empty children when submitting from nested forms>
52
+ serialize_has_many "<name of column>",
53
+ Child, # Child class
54
+ using: JSON, # JSON, YAML, or any other ActiveRecord serializer
55
+ validate: false, # Set true to validate children when validating the parent
56
+ reject_if: Proc.new { ... } # Proc to reject empty children when submitting from nested forms
52
57
  end
53
58
  ```
54
59
 
60
+ ### Works With
61
+
62
+ * Tested on Rails 4.0 and above, Ruby 1.9.3+
63
+ * For the Child model, you can use any class that you want, as long as `attributes` provides a hash, and `new(attributes)` takes that hash. Some options are:
64
+ * [ActiveModel](https://github.com/rails/rails/tree/master/activemodel)
65
+ * [Virtus](https://github.com/solnic/virtus)
66
+ * [ActiveAttr](https://github.com/cgriego/active_attr)
67
+
68
+ ### Advanced Scenarios
69
+
70
+ This gem supports nested relationships in the Child class. You can do it yourself with a simple array attribute, or you can use Virtus which allows you to have explicit nested object types. Just remember that `attributes` should provide a hash (and only a hash, no nested objects), and `new(attributes)` should take the same hash.
71
+
55
72
  ## Contributing
56
73
 
57
74
  1. Fork it ( https://github.com/[my-github-username]/serialize_has_many/fork )
@@ -14,18 +14,14 @@ module SerializeHasMany
14
14
  end
15
15
 
16
16
  def dump(items)
17
- @using.dump to_attributes(items)
18
- case items
19
- when nil then nil
20
- when Array then @using.dump(to_attributes(items))
21
- else raise('not an array or nil')
22
- end
17
+ attributes = to_attributes(items)
18
+ attributes ? @using.dump(attributes) : nil
23
19
  end
24
20
 
25
21
  def from_attributes(items)
26
22
  case items
27
23
  when nil then []
28
- when Array then items.map{ |item| from_item(item) }
24
+ when Array then items.map{ |item| from_hash(item) }
29
25
  else raise('not an array or nil')
30
26
  end
31
27
  end
@@ -33,12 +29,12 @@ module SerializeHasMany
33
29
  def to_attributes(items)
34
30
  case items
35
31
  when nil then nil
36
- when Array then items.map{ |item| to_item(item) }
32
+ when Array then items.map{ |item| to_hash(item) }
37
33
  else raise('not an array or nil')
38
34
  end
39
35
  end
40
36
 
41
- def from_item(item)
37
+ def from_hash(item)
42
38
  case item
43
39
  when nil then nil
44
40
  when Hash then @child_class.new(item)
@@ -47,7 +43,7 @@ module SerializeHasMany
47
43
  end
48
44
  end
49
45
 
50
- def to_item(item)
46
+ def to_hash(item)
51
47
  case item
52
48
  when nil then nil
53
49
  when Hash then item
@@ -1,3 +1,3 @@
1
1
  module SerializeHasMany
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serialize_has_many
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Subhas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-03 00:00:00.000000000 Z
11
+ date: 2015-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ~>
81
81
  - !ruby/object:Gem::Version
82
82
  version: 3.1.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: coveralls
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description: Serializes `has_many` relationships into a single column while still
84
98
  doing attributes, validations, callbacks, nested forms and fields_for. Easy NoSQL
85
99
  with ActiveRecord!