active_record_stash 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ .bundle
23
+
24
+ *.sqlite3
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source :gemcutter
2
+ gem 'rake'
3
+ gem 'rspec'
4
+ gem 'jeweler'
5
+ gem 'activesupport', '3.0.0.beta4', :require => 'active_support'
6
+ gem 'ruby-debug'
7
+ gem 'activemodel', '3.0.0.beta4'
8
+ gem 'activerecord', '3.0.0.beta4'
9
+ gem "sqlite3-ruby", "~> 1.3.0", :require => 'sqlite3'
10
+
data/Gemfile.lock ADDED
@@ -0,0 +1,81 @@
1
+ ---
2
+ dependencies:
3
+ ruby-debug:
4
+ group:
5
+ - :default
6
+ version: ">= 0"
7
+ activerecord:
8
+ group:
9
+ - :default
10
+ version: = 3.0.0.beta4
11
+ sqlite3-ruby:
12
+ group:
13
+ - :default
14
+ version: ~> 1.3.0
15
+ require:
16
+ - sqlite3
17
+ rake:
18
+ group:
19
+ - :default
20
+ version: ">= 0"
21
+ rspec:
22
+ group:
23
+ - :default
24
+ version: ">= 0"
25
+ activemodel:
26
+ group:
27
+ - :default
28
+ version: = 3.0.0.beta4
29
+ jeweler:
30
+ group:
31
+ - :default
32
+ version: ">= 0"
33
+ activesupport:
34
+ group:
35
+ - :default
36
+ version: = 3.0.0.beta4
37
+ require:
38
+ - active_support
39
+ specs:
40
+ - rake:
41
+ version: 0.8.7
42
+ - activesupport:
43
+ version: 3.0.0.beta4
44
+ - builder:
45
+ version: 2.1.2
46
+ - i18n:
47
+ version: 0.4.1
48
+ - activemodel:
49
+ version: 3.0.0.beta4
50
+ - arel:
51
+ version: 0.4.0
52
+ - tzinfo:
53
+ version: 0.3.22
54
+ - activerecord:
55
+ version: 3.0.0.beta4
56
+ - columnize:
57
+ version: 0.3.1
58
+ - json_pure:
59
+ version: 1.4.3
60
+ - gemcutter:
61
+ version: 0.5.0
62
+ - git:
63
+ version: 1.2.5
64
+ - rubyforge:
65
+ version: 2.0.4
66
+ - jeweler:
67
+ version: 1.4.0
68
+ - linecache:
69
+ version: "0.43"
70
+ - rspec:
71
+ version: 1.3.0
72
+ - ruby-debug-base:
73
+ version: 0.10.3
74
+ - ruby-debug:
75
+ version: 0.10.3
76
+ - sqlite3-ruby:
77
+ version: 1.3.0
78
+ hash: b66e7e72c6405329cc80ca86e5160d6378e77f7f
79
+ sources:
80
+ - Rubygems:
81
+ uri: http://gemcutter.org
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Stefan Penner
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,42 @@
1
+ = Schemaless Attributes on an ActiveRecord model
2
+
3
+ Simple little ActiveRecord plugin which allows a subset of a models attributes to reside in a serialized column, while still working with form helpers and validators.
4
+
5
+ # Assuming a Email schema looks like
6
+ create_table "emails", :force => true do |t|
7
+ t.string "email"
8
+ t.datetime "created_at"
9
+ t.datetime "updated_at"
10
+ t.text "data"
11
+ end
12
+
13
+ # we can invoke the stash helper as follows
14
+ stash <fields..>, :in => <column to stash the fields into>
15
+
16
+ #full example
17
+ class Email < ActiveRecord::Base
18
+ validates_presence_of :name, :phone, :email, :content
19
+
20
+ stash :phone, :name, :content :in => :data
21
+ end
22
+
23
+ # validations, and form helpers now work on stashed attributes
24
+
25
+ = Getting Started
26
+
27
+ #Gemfile
28
+ gem "active_record_stash"
29
+
30
+ == Note on Patches/Pull Requests
31
+
32
+ * Fork the project.
33
+ * Make your feature addition or bug fix.
34
+ * Add tests for it. This is important so I don't break it in a
35
+ future version unintentionally.
36
+ * Commit, do not mess with rakefile, version, or history.
37
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
38
+ * Send me a pull request. Bonus points for topic branches.
39
+
40
+ == Copyright
41
+
42
+ Copyright (c) 2010 Stefan Penner. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "active_record_stash"
8
+ gem.summary = %Q{allow attributes stored in serialized columns to be first class active record attributes}
9
+ gem.description = %Q{provide validations and form helpers for attributes stored in serialized ActiveRecord column}
10
+ gem.email = "stefan.penner@gmail.com"
11
+ gem.homepage = "http://github.com/stefanpenner/active_record_stash"
12
+ gem.authors = ["Stefan Penner"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "active_record_stash #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,58 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{active_record_stash}
8
+ s.version = "0.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Stefan Penner"]
12
+ s.date = %q{2010-06-20}
13
+ s.description = %q{provide validations and form helpers for attributes stored in serialized ActiveRecord column}
14
+ s.email = %q{stefan.penner@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "active_record_stash.gemspec",
29
+ "init.rb",
30
+ "lib/active_record_stash.rb",
31
+ "spec/active_record_stash_spec.rb",
32
+ "spec/spec.opts",
33
+ "spec/spec_helper.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/stefanpenner/active_record_stash}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.7}
39
+ s.summary = %q{allow attributes stored in serialized columns to be first class active record attributes}
40
+ s.test_files = [
41
+ "spec/active_record_stash_spec.rb",
42
+ "spec/spec_helper.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
50
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
51
+ else
52
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
56
+ end
57
+ end
58
+
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'active_record_stash'
@@ -0,0 +1,62 @@
1
+ module ActiveRecord
2
+ module Stash
3
+
4
+ extend ActiveSupport::Concern
5
+
6
+ NO_TARGET_ERROR = "stashing needs a target serialized column. Supply an options hash with a :in key as the last argument (e.g. stash :apple, :in => :greeter)."
7
+
8
+ included do
9
+ class_inheritable_accessor :stashed_attributes
10
+ self.stashed_attributes = Hash.new([])
11
+
12
+ after_initialize :_load_stashed_attributes
13
+ before_save :_stash_attributes
14
+ end
15
+
16
+ private
17
+ def _load_stashed_attributes
18
+ stashed_attributes.each_pair do |store_name,methods|
19
+ store = send(store_name)
20
+
21
+ next unless store
22
+
23
+ methods.each do |method|
24
+ send :"#{method}=", store[method] unless send(method)
25
+ end
26
+
27
+ end
28
+ end
29
+
30
+ def _stash_attributes
31
+ stashed_attributes.each_pair do |store_name,methods|
32
+
33
+ eval "self.#{store_name} ||= {}"
34
+
35
+ methods.each do |method|
36
+ send(store_name)[method] = send method
37
+ end
38
+
39
+ end
40
+ end
41
+
42
+ module ClassMethods
43
+
44
+ def stash *methods
45
+ options = methods.extract_options!
46
+ options.assert_valid_keys(:in)
47
+ options.symbolize_keys!
48
+
49
+ unless serialized_column = options[:in]
50
+ raise ArgumentError, NO_TARGET_ERROR
51
+ end
52
+
53
+ stashed_attributes[serialized_column] += methods.map(&:to_sym)
54
+
55
+ serialize serialized_column
56
+ attr_accessor *methods
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ ActiveRecord::Base.send(:include, ActiveRecord::Stash)
@@ -0,0 +1,134 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ conn = {
4
+ :adapter => 'sqlite3',
5
+ :database => 'activerecord_unittest',
6
+ :database => 'spec/testing.sqlite3',
7
+ :encoding => 'utf8'
8
+ }
9
+
10
+ ActiveRecord::Base.establish_connection(conn)
11
+
12
+ class Email < ActiveRecord::Base
13
+ connection.create_table :emails, :force => true do |t|
14
+ t.string :name
15
+ t.text :data
16
+ t.timestamps
17
+ end
18
+
19
+ # validates_presense_of :name
20
+ stash :phone, :address, :postal_code, :in => :data
21
+ end
22
+
23
+ describe ActiveRecord::Stash do
24
+ describe "new object initialization" do
25
+ before(:all) do
26
+ @email = Email.new(
27
+ :phone => "123456789",
28
+ :address => "1234 Apple way",
29
+ :postal_code => "13244"
30
+ )
31
+ end
32
+
33
+ it "is a valid object" do
34
+ @email.should be_valid
35
+ end
36
+
37
+ it "has all the correct forward facing attributes" do
38
+ @email.phone.should match("123456789")
39
+ @email.address.should match("1234 Apple way")
40
+ @email.postal_code.should match("13244")
41
+ end
42
+
43
+ it "has an empty data column" do
44
+ @email.data.should be_nil
45
+ end
46
+
47
+ describe "save" do
48
+ it "is successful" do
49
+ @email.save.should be_true
50
+ end
51
+
52
+ it "still has the correct forward facing attributes" do
53
+ @email.phone.should match("123456789")
54
+ @email.address.should match("1234 Apple way")
55
+ @email.postal_code.should match("13244")
56
+ end
57
+
58
+ it "does not have an empty data column" do
59
+ @email.data.should_not be_empty
60
+ end
61
+
62
+ it "has the correct serialized data in the serialized column" do
63
+
64
+ @email.data.should eql({
65
+ :phone => "123456789",
66
+ :address => "1234 Apple way",
67
+ :postal_code => "13244"
68
+ })
69
+ end
70
+ end
71
+ end
72
+
73
+ describe "object creation" do
74
+ before :all do
75
+ @email = Email.create(
76
+ :phone => "123456789",
77
+ :address => "1234 Apple way",
78
+ :postal_code => "13244"
79
+ )
80
+ end
81
+
82
+ it "is a valid object" do
83
+ @email.should be_valid
84
+ end
85
+
86
+ it "still has the correct forward facing attributes" do
87
+ @email.phone.should match("123456789")
88
+ @email.address.should match("1234 Apple way")
89
+ @email.postal_code.should match("13244")
90
+ end
91
+ it "does not have an empty data column" do
92
+ @email.data.should_not be_empty
93
+ end
94
+
95
+ it "has the correct serialized data in the serialized column" do
96
+
97
+ @email.data.should eql({
98
+ :phone => "123456789",
99
+ :address => "1234 Apple way",
100
+ :postal_code => "13244"
101
+ })
102
+ end
103
+
104
+ describe "#update_attributes" do
105
+ before :all do
106
+ @email.update_attributes({
107
+ :phone => "2222222",
108
+ :address => "4321 yaw elppa"
109
+ })
110
+ end
111
+ it "has no errors" do
112
+ @email.errors.any?.should be_false
113
+ end
114
+
115
+ it "does not have an empty data column" do
116
+ @email.data.should_not be_empty
117
+ end
118
+
119
+ it "has all the expected contents of the data column" do
120
+ @email.data.should eql({
121
+ :phone => "2222222",
122
+ :address => "4321 yaw elppa",
123
+ :postal_code => "13244"
124
+ })
125
+ end
126
+
127
+ it "still has the correct forward facing attributes" do
128
+ @email.phone.should match("2222222")
129
+ @email.address.should match("4321 yaw elppa")
130
+ @email.postal_code.should match("13244")
131
+ end
132
+ end
133
+ end
134
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,18 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'rubygems'
5
+ require 'bundler'
6
+
7
+ Bundler.setup
8
+
9
+ require 'active_support/concern'
10
+ require 'active_record'
11
+ require 'active_model'
12
+ require 'active_record_stash'
13
+ require 'spec'
14
+ require 'spec/autorun'
15
+
16
+ Spec::Runner.configure do |config|
17
+
18
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record_stash
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 0
10
+ version: 0.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Stefan Penner
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-06-20 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ description: provide validations and form helpers for attributes stored in serialized ActiveRecord column
38
+ email: stefan.penner@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - LICENSE
45
+ - README.rdoc
46
+ files:
47
+ - .document
48
+ - .gitignore
49
+ - Gemfile
50
+ - Gemfile.lock
51
+ - LICENSE
52
+ - README.rdoc
53
+ - Rakefile
54
+ - VERSION
55
+ - active_record_stash.gemspec
56
+ - init.rb
57
+ - lib/active_record_stash.rb
58
+ - spec/active_record_stash_spec.rb
59
+ - spec/spec.opts
60
+ - spec/spec_helper.rb
61
+ has_rdoc: true
62
+ homepage: http://github.com/stefanpenner/active_record_stash
63
+ licenses: []
64
+
65
+ post_install_message:
66
+ rdoc_options:
67
+ - --charset=UTF-8
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ requirements: []
89
+
90
+ rubyforge_project:
91
+ rubygems_version: 1.3.7
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: allow attributes stored in serialized columns to be first class active record attributes
95
+ test_files:
96
+ - spec/active_record_stash_spec.rb
97
+ - spec/spec_helper.rb