serialize-list-attribute 0.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.
- data/.gitignore +18 -0
- data/.travis.yml +4 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +54 -0
- data/Rakefile +1 -0
- data/lib/serialize-list-attribute.rb +38 -0
- data/lib/serialize-list-attribute/version.rb +3 -0
- data/serialize-list-attribute.gemspec +22 -0
- data/spec/serialize_list_attribute_spec.rb +45 -0
- data/spec/support/active_record.rb +27 -0
- data/spec/support/spec_helper.rb +6 -0
- metadata +116 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Alexander Kaupanin
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# Serialize attributes like a boss 
|
2
|
+
|
3
|
+
This gem adds helper methods to serialize AR model simple text attribute to an array or hash,
|
4
|
+
handles the field updates and allows to perform per-item validation.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'serialize-list-attribute'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install serialize-list-attribute
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
Example: we store emails recipients as simple text field, but want to operate them as it was an array and,
|
22
|
+
into the bargain, perform internal validation on create/update.
|
23
|
+
|
24
|
+
In a model:
|
25
|
+
```ruby
|
26
|
+
class MailingList < ActiveRecord::Base
|
27
|
+
attr_accessible :recipients
|
28
|
+
|
29
|
+
include SerializeListAttribute
|
30
|
+
serialize_list_attributes :recipients, :recipients_emails_funnel_report, validation_method: :validate_for_email
|
31
|
+
|
32
|
+
# validation method which is to be invoked on each recipients array item
|
33
|
+
# consider email? method returns false if string is not a valid email address
|
34
|
+
def validate_for_email value, column
|
35
|
+
unless value.email?
|
36
|
+
errors.add column, 'Your validation error message'
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
In a view:
|
43
|
+
```ruby
|
44
|
+
= semantic_form_for resource do |f|
|
45
|
+
= f.input :raw_recipients, as: :text
|
46
|
+
```
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
1. Fork it
|
51
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
52
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
53
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
54
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module SerializeListAttribute
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
def self.serialize_list_attributes *contexts
|
6
|
+
options = contexts.extract_options!
|
7
|
+
as = options[:as].to_s.camelize.constantize || Array
|
8
|
+
before_save :synchronise_lists
|
9
|
+
|
10
|
+
contexts.each do |context|
|
11
|
+
serialize "#{context}", as
|
12
|
+
attr_writer "raw_#{context}"
|
13
|
+
|
14
|
+
define_method "raw_#{context}" do
|
15
|
+
instance_variable_get("@raw_#{context}") || instance_variable_set("@raw_#{context}", send("#{context}").join(', '))
|
16
|
+
end
|
17
|
+
|
18
|
+
define_method "build_#{context}" do
|
19
|
+
send("raw_#{context}").split(',').map(&:strip)
|
20
|
+
end
|
21
|
+
|
22
|
+
unless options[:validation_method].nil?
|
23
|
+
before_validation do
|
24
|
+
send("build_#{context}").each do |item|
|
25
|
+
send options[:validation_method], item, "raw_#{context}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
define_method :synchronise_lists do
|
32
|
+
contexts.each do |context|
|
33
|
+
send "#{context}=", send("build_#{context}")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'serialize-list-attribute/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = 'serialize-list-attribute'
|
8
|
+
s.version = SerializeListAttribute::VERSION
|
9
|
+
s.authors = ['Alexander Kaupanin']
|
10
|
+
s.email = %w(kaupanin@gmail.com)
|
11
|
+
s.description = %q{Manage text field attribute serialized as an array, hash, etc. Add per-item validation}
|
12
|
+
s.summary = %q{Manage text field attribute serialized as an array, hash, etc. Add per-item validation}
|
13
|
+
s.homepage = 'http://github.com/simsalabim/serialize-list-attribute'
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split($/)
|
16
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
17
|
+
s.require_paths = %w(lib)
|
18
|
+
|
19
|
+
s.add_development_dependency 'rspec', '~> 2.9'
|
20
|
+
s.add_development_dependency 'sqlite3'
|
21
|
+
s.add_runtime_dependency 'activerecord', '~> 3.0'
|
22
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'support/spec_helper'
|
2
|
+
require 'support/active_record'
|
3
|
+
require 'serialize-list-attribute'
|
4
|
+
|
5
|
+
class MailingList < ActiveRecord::Base
|
6
|
+
attr_accessible :recipients
|
7
|
+
|
8
|
+
def validate_length item, column
|
9
|
+
errors.add column, 'Should be 5 characters or longer!' if item.length < 5
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe MailingList do
|
14
|
+
|
15
|
+
it 'should have plain text recipients attribute default' do
|
16
|
+
subject.recipients = 'cyberdyne_systems@model.101, the@terminator.com'
|
17
|
+
subject.recipients.is_a?(String).should be_true
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should have recipients attribute serialized into an array if the gem is used with defaults' do
|
21
|
+
subject.class.send :include, SerializeListAttribute
|
22
|
+
subject.class.send :serialize_list_attributes, :recipients
|
23
|
+
subject.recipients = %w(cyberdyne_systems@model.101 the@terminator.com)
|
24
|
+
|
25
|
+
subject.recipients.value.is_a?(Array).should be_true
|
26
|
+
subject.should have(:no).errors_on(:raw_recipients)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should have recipients attribute serialized into a hash if the gem is configured properly' do
|
30
|
+
subject.class.send :include, SerializeListAttribute
|
31
|
+
subject.class.send :serialize_list_attributes, :recipients, as: :hash
|
32
|
+
subject.recipients = { person: 'cyberdyne_systems@model.101', nickname: 'the@terminator.com' }
|
33
|
+
|
34
|
+
subject.recipients.value.is_a?(Hash).should be_true
|
35
|
+
subject.should have(:no).errors_on(:raw_recipients)
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should validate each recipients attribute particle if the gem is configured to do so' do
|
39
|
+
subject.class.send :include, SerializeListAttribute
|
40
|
+
subject.class.send :serialize_list_attributes, :recipients, validation_method: :validate_length
|
41
|
+
|
42
|
+
subject.raw_recipients = 'arnie, 12'
|
43
|
+
subject.should have(1).error_on(:raw_recipients)
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
|
4
|
+
|
5
|
+
ActiveRecord::Migration.create_table :mailing_lists do |t|
|
6
|
+
t.text :recipients
|
7
|
+
end
|
8
|
+
|
9
|
+
module ActiveModel::Validations
|
10
|
+
# Extension to enhance `should have` on AR Model instances. Calls
|
11
|
+
# model.valid? in order to prepare the object's errors object.
|
12
|
+
#
|
13
|
+
# You can also use this to specify the content of the error messages.
|
14
|
+
#
|
15
|
+
# @example
|
16
|
+
#
|
17
|
+
# model.should have(:no).errors_on(:attribute)
|
18
|
+
# model.should have(1).error_on(:attribute)
|
19
|
+
# model.should have(n).errors_on(:attribute)
|
20
|
+
#
|
21
|
+
# model.errors_on(:attribute).should include("can't be blank")
|
22
|
+
def errors_on(attribute)
|
23
|
+
self.valid?
|
24
|
+
[self.errors[attribute]].flatten.compact
|
25
|
+
end
|
26
|
+
alias :error_on :errors_on
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: serialize-list-attribute
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alexander Kaupanin
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-06 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.9'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.9'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: sqlite3
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: activerecord
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '3.0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
description: Manage text field attribute serialized as an array, hash, etc. Add per-item
|
63
|
+
validation
|
64
|
+
email:
|
65
|
+
- kaupanin@gmail.com
|
66
|
+
executables: []
|
67
|
+
extensions: []
|
68
|
+
extra_rdoc_files: []
|
69
|
+
files:
|
70
|
+
- .gitignore
|
71
|
+
- .travis.yml
|
72
|
+
- Gemfile
|
73
|
+
- LICENSE.txt
|
74
|
+
- README.md
|
75
|
+
- Rakefile
|
76
|
+
- lib/serialize-list-attribute.rb
|
77
|
+
- lib/serialize-list-attribute/version.rb
|
78
|
+
- serialize-list-attribute.gemspec
|
79
|
+
- spec/serialize_list_attribute_spec.rb
|
80
|
+
- spec/support/active_record.rb
|
81
|
+
- spec/support/spec_helper.rb
|
82
|
+
homepage: http://github.com/simsalabim/serialize-list-attribute
|
83
|
+
licenses: []
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
segments:
|
95
|
+
- 0
|
96
|
+
hash: 69075439
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ! '>='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
segments:
|
104
|
+
- 0
|
105
|
+
hash: 69075439
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 1.8.24
|
109
|
+
signing_key:
|
110
|
+
specification_version: 3
|
111
|
+
summary: Manage text field attribute serialized as an array, hash, etc. Add per-item
|
112
|
+
validation
|
113
|
+
test_files:
|
114
|
+
- spec/serialize_list_attribute_spec.rb
|
115
|
+
- spec/support/active_record.rb
|
116
|
+
- spec/support/spec_helper.rb
|