activerecord-attribute_override 0.0.2
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/README.md +59 -0
- data/Rakefile +8 -0
- data/lib/activerecord-attribute_override.rb +26 -0
- data/lib/activerecord-attribute_override/version.rb +3 -0
- metadata +67 -0
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
ActiveRecord - Attribute Override
|
2
|
+
=====
|
3
|
+
|
4
|
+
A gem that adds two methods to ActiveRecord::Base that let you override attributes of an ActiveRecord model instance much more easily. It also allows you to specify the Rails environments in which the attributes should be overriden.
|
5
|
+
|
6
|
+
The attributes are overriden only in the specified model instance.
|
7
|
+
|
8
|
+
### Setup
|
9
|
+
|
10
|
+
In your Rails 3+ project, add this to your Gemfile:
|
11
|
+
|
12
|
+
gem 'activerecord-attribute-override', :git => 'git://github.com/garysweaver/activerecord-attribute_override.git'
|
13
|
+
|
14
|
+
Then run:
|
15
|
+
|
16
|
+
bundle install
|
17
|
+
|
18
|
+
### Usage
|
19
|
+
|
20
|
+
Override my_attribute_name method in my_model instance to return 'some value':
|
21
|
+
|
22
|
+
my_model.override_attribute(:my_attribute_name, 'some value')
|
23
|
+
|
24
|
+
Override my_attribute_name method in my_model instance to return 'some value' only when Rails.env is 'development':
|
25
|
+
|
26
|
+
my_model.override_attribute(:my_attribute_name, 'some value', :development)
|
27
|
+
|
28
|
+
Override my_attribute_name method in my_model instance to return 'some value' only when Rails.env is 'test' or 'production':
|
29
|
+
|
30
|
+
my_model.override_attribute(:my_attribute_name, 'some value', [:test, :production])
|
31
|
+
|
32
|
+
Override my_attribute_name and my_other_attribute_name:
|
33
|
+
|
34
|
+
my_model.override_attributes({my_attribute_name: 'some value', my_other_attribute_name: 'some other value'})
|
35
|
+
|
36
|
+
Override my_attribute_name and my_other_attribute_name only when Rails.env is 'development':
|
37
|
+
|
38
|
+
my_model.override_attributes({my_attribute_name: 'some value', my_other_attribute_name: 'some other value'}, :development)
|
39
|
+
|
40
|
+
Override my_attribute_name and my_other_attribute_name only when Rails.env is 'test' or 'production':
|
41
|
+
|
42
|
+
my_model.override_attributes({my_attribute_name: 'some value', my_other_attribute_name: 'some other value'}, [:test, :production])
|
43
|
+
|
44
|
+
#### Using Without Rails
|
45
|
+
|
46
|
+
To use without Rails, either don't specify the environment(s), or define your own Rails.env outside of Rails to return the environment of your choice like:
|
47
|
+
|
48
|
+
class Rails
|
49
|
+
def self.env=(e); @@e = e; end
|
50
|
+
def self.env; @@e; end
|
51
|
+
end
|
52
|
+
|
53
|
+
Rails.env = :foobar
|
54
|
+
|
55
|
+
### License
|
56
|
+
|
57
|
+
Copyright (c) 2012 Gary S. Weaver, released under the [MIT license][lic].
|
58
|
+
|
59
|
+
[lic]: http://github.com/garysweaver/activerecord-attribute-override/blob/master/LICENSE
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require "activerecord-attribute_override/version"
|
2
|
+
|
3
|
+
# monkey-patch ActiveRecord::Base
|
4
|
+
module ActiveRecord
|
5
|
+
class Base
|
6
|
+
def override_attribute(attr_name, value, environments = [])
|
7
|
+
# raise error for common method naming issue to point users in right direction
|
8
|
+
raise "Cannot 'override_attribute' with hash as the attribute name. Use 'override_attributes' instead." if attr_name.is_a?(Hash)
|
9
|
+
environments = [environments] if !environments.is_a?(Array)
|
10
|
+
if environments.empty? || environments.collect{|e|e.to_sym}.include?(Rails.env.to_sym)
|
11
|
+
instance_variable_set("@#{attr_name}".to_sym, value)
|
12
|
+
instance_eval "def #{attr_name}; @#{attr_name}; end"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def override_attributes(attr_name_to_value_map, environments = [])
|
17
|
+
environments = [environments] if !environments.is_a?(Array)
|
18
|
+
if environments.empty? || environments.collect{|e|e.to_sym}.include?(Rails.env.to_sym)
|
19
|
+
attr_name_to_value_map.keys.each do |attr_name|
|
20
|
+
instance_variable_set("@#{attr_name}".to_sym, attr_name_to_value_map[attr_name])
|
21
|
+
instance_eval "def #{attr_name}; @#{attr_name}; end"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-attribute_override
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Gary S. Weaver
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '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: '0'
|
30
|
+
description: Adds methods to your models to let you easily override instance attributes
|
31
|
+
in specific environments.
|
32
|
+
email:
|
33
|
+
- garysweaver@gmail.com
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- lib/activerecord-attribute_override/version.rb
|
39
|
+
- lib/activerecord-attribute_override.rb
|
40
|
+
- Rakefile
|
41
|
+
- README.md
|
42
|
+
homepage: https://github.com/garysweaver/activerecord-attribute_override
|
43
|
+
licenses:
|
44
|
+
- MIT
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.8.24
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: Lets you easily override instance attributes in activerecord.
|
67
|
+
test_files: []
|