acread 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source 'http://rubygems.org/'
2
+
3
+ # gem dependencies
4
+ gem 'rails', '>= 3.0'
5
+
6
+ # dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "rdoc", ">= 3.12"
11
+ gem "bundler", ">= 1.0.0"
12
+ gem "jeweler", ">= 1.8.4"
13
+ gem 'sqlite3'
14
+ end
15
+
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 yann ARMAND
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.
@@ -0,0 +1,81 @@
1
+ # acread
2
+ [![Build Status](https://secure.travis-ci.org/yarmand/acread.png?branch=master)](http://travis-ci.org/yarmand/acread)
3
+
4
+ acread is a gem that helps you deprecating ActiveRecord attributes.
5
+
6
+ When you deprecate an attribute, acread can helps you in 3 ways :
7
+
8
+ 1. helps you finding where you are using this attribute by creating glue to raise a `DeprecatedAttributeError`.
9
+ 1. ignore this atribute when serializing the object through to_json, to_xml ...
10
+ 1. helps your zero downtime migration by ignoring the attribute for objects already in memory when saving to database.
11
+
12
+ # Usage
13
+
14
+ ## Installation
15
+ add to your Gemfile :
16
+
17
+ gem 'acread'
18
+
19
+ ## deprecate an attribute
20
+
21
+ class Person < ActiveRecord::Base
22
+ ...
23
+ deprecate_attribute :long_name
24
+ ...
25
+ end
26
+
27
+ ## find attribute usage
28
+ you can catch the `DeprecatedAttributeError` exception and for example put a backtrace in a specific logger.
29
+
30
+ class ApplicationController
31
+ rescue_from DeprecatedAttributeError, :with => :log_deprecate
32
+
33
+ private
34
+
35
+ def deprecated_logger
36
+ @@deprecated_logger ||= Logger.new("#{Rails.root}/log/deprecated_calls.log")
37
+ end
38
+
39
+ def log_deprecated e
40
+ deprecated_logger.error(e.stacktrace.join("\n"))
41
+ end
42
+ end
43
+
44
+ ## zero downtime migration
45
+ When you are done with cleaning your code from any usage of deprecated attributes, you can prepare a migration including some drop_columns.
46
+
47
+ example :
48
+
49
+ class RemoveLongNames < ActiveRecord::Migration
50
+ def self.up
51
+ remove_column :Person, :long_name
52
+
53
+ end
54
+
55
+ def self.down
56
+ raise ActiveRecord::IrreversibleMigration
57
+ end
58
+ end
59
+
60
+ Then you can safely follow the steps :
61
+
62
+
63
+ 1. Deploy your code with deprecation declaration in it
64
+ 1. Run the migration
65
+ 2. Remove deprecation declarations from your code
66
+ 3. Deploy your final clean version of code
67
+
68
+ # Contributing to acread
69
+
70
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
71
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
72
+ * Fork the project.
73
+ * Start a feature/bugfix branch.
74
+ * Commit and push until you are happy with your contribution.
75
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
76
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
77
+
78
+ # Copyright
79
+
80
+ Copyright (c) 2012 yann ARMAND under MIT See LICENSE.txt for
81
+ further details.
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "acread"
18
+ gem.homepage = "http://github.com/yarmand/acread"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{An ActiveRecord Extension to deprecate attributes}
21
+ gem.description = %Q{When you deprecate an attribute, acread can helps you in 3 ways :
22
+
23
+ 1. helps you finding where you are using this attribute by creating glue to raise a `DeprecatedAttributeError`.
24
+ 2. ignore this atribute when serializing the object through to_json, to_xml ...
25
+ 3. helps your zero downtime migration by ignoring the attribute for objects already in memory when saving to database.}
26
+ gem.email = "yann@harakys.com"
27
+ gem.authors = ["yann ARMAND"]
28
+ # dependencies defined in Gemfile
29
+ end
30
+ Jeweler::RubygemsDotOrgTasks.new
31
+
32
+ require 'rake/testtask'
33
+ Rake::TestTask.new(:test) do |test|
34
+ test.libs << 'lib' << 'test'
35
+ test.pattern = 'test/**/test_*.rb'
36
+ test.verbose = true
37
+ end
38
+
39
+ task :default => :test
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "acread #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.5.0
@@ -0,0 +1,65 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "acread"
8
+ s.version = "0.5.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["yann ARMAND"]
12
+ s.date = "2012-10-23"
13
+ s.description = "When you deprecate an attribute, acread can helps you in 3 ways :\n\n1. helps you finding where you are using this attribute by creating glue to raise a `DeprecatedAttributeError`.\n2. ignore this atribute when serializing the object through to_json, to_xml ...\n3. helps your zero downtime migration by ignoring the attribute for objects already in memory when saving to database."
14
+ s.email = "yann@harakys.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ "Gemfile",
21
+ "LICENSE.txt",
22
+ "README.md",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "acread.gemspec",
26
+ "lib/acread.rb",
27
+ "lib/acread/active_record_ext.rb",
28
+ "lib/acread/deprecatable.rb",
29
+ "test/helper.rb",
30
+ "test/test_acread.rb"
31
+ ]
32
+ s.homepage = "http://github.com/yarmand/acread"
33
+ s.licenses = ["MIT"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = "1.8.24"
36
+ s.summary = "An ActiveRecord Extension to deprecate attributes"
37
+
38
+ if s.respond_to? :specification_version then
39
+ s.specification_version = 3
40
+
41
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
+ s.add_runtime_dependency(%q<rails>, [">= 3.0"])
43
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
44
+ s.add_development_dependency(%q<rdoc>, [">= 3.12"])
45
+ s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
46
+ s.add_development_dependency(%q<jeweler>, [">= 1.8.4"])
47
+ s.add_development_dependency(%q<sqlite3>, [">= 0"])
48
+ else
49
+ s.add_dependency(%q<rails>, [">= 3.0"])
50
+ s.add_dependency(%q<shoulda>, [">= 0"])
51
+ s.add_dependency(%q<rdoc>, [">= 3.12"])
52
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
53
+ s.add_dependency(%q<jeweler>, [">= 1.8.4"])
54
+ s.add_dependency(%q<sqlite3>, [">= 0"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<rails>, [">= 3.0"])
58
+ s.add_dependency(%q<shoulda>, [">= 0"])
59
+ s.add_dependency(%q<rdoc>, [">= 3.12"])
60
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
61
+ s.add_dependency(%q<jeweler>, [">= 1.8.4"])
62
+ s.add_dependency(%q<sqlite3>, [">= 0"])
63
+ end
64
+ end
65
+
@@ -0,0 +1,2 @@
1
+ require 'acread/active_record_ext'
2
+
@@ -0,0 +1,20 @@
1
+ require 'active_record'
2
+ require 'acread/deprecatable'
3
+
4
+ class ActiveRecord::Base
5
+ extend Deprecatable
6
+
7
+ def columns
8
+ self.class.columns.reject { |c| (self.class.deprecated_attributes || []).include? c.name.to_s}
9
+ end
10
+
11
+ # ensure the deprecated attributes will be skip when serialize the record
12
+ def serializable_hash(options = {})
13
+ options = {} if options.nil?
14
+ options = {
15
+ :only => self.attributes.keys.map(&:to_sym) - (self.class.deprecated_attributes || []).map(&:to_sym)
16
+ }.update(options)
17
+ ActiveRecord::Serialization.instance_method( :serializable_hash ).bind( self ).call(options)
18
+ end
19
+
20
+ end
@@ -0,0 +1,25 @@
1
+ module Deprecatable
2
+
3
+ def deprecate_attribute attr
4
+ @deprecated_attributes ||=[]
5
+ @deprecated_attributes << attr.to_s
6
+ overide_accessors attr
7
+ end
8
+
9
+ def deprecated_attributes
10
+ @deprecated_attributes
11
+ end
12
+
13
+ def overide_accessors attr
14
+ msg = "You can't access atribute #{attr}, it has been deprecated"
15
+ [ '', '=', '?', '_changed?'].each do |term|
16
+ define_method("#{attr}#{term}") do |e=nil|
17
+ raise DeprecatedAttributeError, msg
18
+ end
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ class DeprecatedAttributeError < RuntimeError
25
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'acread'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,69 @@
1
+ require 'helper'
2
+
3
+ class Person < ActiveRecord::Base
4
+ deprecate_attribute :long_name
5
+ establish_connection :adapter => 'sqlite3', :database => 'foobar.db'
6
+ connection.create_table table_name, :force => true do |t|
7
+ t.string :name
8
+ t.string :long_name
9
+ end
10
+ end
11
+
12
+ class PersonFull < ActiveRecord::Base
13
+ establish_connection :adapter => 'sqlite3', :database => 'foobar.db'
14
+ connection.create_table table_name, :force => true do |t|
15
+ t.string :name
16
+ t.string :long_name
17
+ end
18
+ end
19
+
20
+
21
+ class TestAcread < Test::Unit::TestCase
22
+ def setup
23
+ @bob = Person.create!(:name => 'bob')
24
+ @james = PersonFull.create(:long_name => 'James the magnific')
25
+ end
26
+
27
+ def test_cretor_with_deprecated_field_raise_exception
28
+ assert_raise(DeprecatedAttributeError) { Person.create(:long_name => 'should not see this') }
29
+ end
30
+
31
+ def test_read_deprecated_raise_exception
32
+ assert_raise(DeprecatedAttributeError) { @bob.long_name }
33
+ end
34
+
35
+ def test_write_deprecated_raise_exception
36
+ assert_raise(DeprecatedAttributeError) { @bob.long_name = 'Bon' }
37
+ end
38
+
39
+
40
+ def test_hash_exclude_deprecated_attributes
41
+ h = @bob.serializable_hash
42
+ assert !(h.keys.include? 'long_name')
43
+ end
44
+
45
+ def test_columns_exclude_deprecated_attributes
46
+ h = @bob.send(:columns).map(&:name)
47
+ assert !(h.include? 'long_name')
48
+ end
49
+
50
+ def test_non_deprecated_class_can_read_all_attributes
51
+ assert @james.long_name.is_a? String
52
+ end
53
+
54
+ def test_non_deprecated_class_can_write_all_attributes
55
+ s = 'james the short'
56
+ @james.long_name = s
57
+ assert @james.long_name == s
58
+ end
59
+
60
+ def test_non_deprecated_class_columns_output_all_attributes
61
+ h = @james.send(:columns).map(&:name)
62
+ assert h.include? 'long_name'
63
+ end
64
+
65
+ def test_non_deprecated_class_hash_output_all_attributes
66
+ h = @james.serializable_hash
67
+ assert h.keys.include? 'long_name'
68
+ end
69
+ end
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acread
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - yann ARMAND
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: shoulda
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: rdoc
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '3.12'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '3.12'
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 1.0.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 1.0.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: jeweler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: 1.8.4
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: 1.8.4
94
+ - !ruby/object:Gem::Dependency
95
+ name: sqlite3
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: ! 'When you deprecate an attribute, acread can helps you in 3 ways :
111
+
112
+
113
+ 1. helps you finding where you are using this attribute by creating glue to raise
114
+ a `DeprecatedAttributeError`.
115
+
116
+ 2. ignore this atribute when serializing the object through to_json, to_xml ...
117
+
118
+ 3. helps your zero downtime migration by ignoring the attribute for objects already
119
+ in memory when saving to database.'
120
+ email: yann@harakys.com
121
+ executables: []
122
+ extensions: []
123
+ extra_rdoc_files:
124
+ - LICENSE.txt
125
+ - README.md
126
+ files:
127
+ - Gemfile
128
+ - LICENSE.txt
129
+ - README.md
130
+ - Rakefile
131
+ - VERSION
132
+ - acread.gemspec
133
+ - lib/acread.rb
134
+ - lib/acread/active_record_ext.rb
135
+ - lib/acread/deprecatable.rb
136
+ - test/helper.rb
137
+ - test/test_acread.rb
138
+ homepage: http://github.com/yarmand/acread
139
+ licenses:
140
+ - MIT
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ! '>='
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ segments:
152
+ - 0
153
+ hash: -2680666247373849907
154
+ required_rubygems_version: !ruby/object:Gem::Requirement
155
+ none: false
156
+ requirements:
157
+ - - ! '>='
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ requirements: []
161
+ rubyforge_project:
162
+ rubygems_version: 1.8.24
163
+ signing_key:
164
+ specification_version: 3
165
+ summary: An ActiveRecord Extension to deprecate attributes
166
+ test_files: []