IPGlider-ImmutableAttributes 1.0.3.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/Manifest.txt +9 -0
- data/README +26 -0
- data/Rakefile +9 -0
- data/init.rb +2 -0
- data/install.rb +1 -0
- data/lib/immutable_attributes.rb +61 -0
- data/rails/init.rb +2 -0
- data/test/immutable_attributes_test.rb +31 -0
- data/uninstall.rb +1 -0
- metadata +72 -0
data/Manifest.txt
ADDED
data/README
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
ImmutableAttributes
|
|
4
|
+
===================
|
|
5
|
+
|
|
6
|
+
When you want to prevent certain attributes from being changed once set you can declare them as immutable:
|
|
7
|
+
|
|
8
|
+
class MyModel < ActiveRecord::Base
|
|
9
|
+
attr_immutable :permalink, :param_identifier
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
When MyModel.find(:first).permalink = 'anything' is called it will raise an ImmutableAttributeError
|
|
13
|
+
MyModel.new.permalink = 'works!' will properly set the value because the record is unsaved.
|
|
14
|
+
|
|
15
|
+
If you'd only like this to happen for certain conditions, and want to handle other attribute-writers, too (like update_attribute), you can use the validation.
|
|
16
|
+
|
|
17
|
+
validates_immutable :permalink
|
|
18
|
+
|
|
19
|
+
Configuration options for the validation:
|
|
20
|
+
* :message - A customer error message (default is: "can't be changed")
|
|
21
|
+
* :if - Specifies a method, proc, or string to call to determine if the validation should occure (e.g., :if => :allow_validation or :if => Proc.new{|user| user.signup_step > 2}. The method, proc or string should return or evaluate to a true or false value.
|
|
22
|
+
* :unless - Specifies a method, proc, or string to call to determine if the validation should not occure (e.g., :unless => :skip_validation or :unless => Proc.new{|user| user.signup_step <= 2}. The method, proc or string should return or evaluate to a true or false value.
|
|
23
|
+
|
|
24
|
+
Created by Jack Danger Canty @ http://6brand.com
|
|
25
|
+
Released under the same licence as Rails (MIT)
|
|
26
|
+
|
data/Rakefile
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'hoe'
|
|
3
|
+
require './lib/immutable_attributes.rb'
|
|
4
|
+
|
|
5
|
+
Hoe.new('immutable_attributes', ImmutableAttributes::VERSION) do |p|
|
|
6
|
+
p.developer('Jack Danger Canty', 'git@6brand.com')
|
|
7
|
+
p.summary = "Immutable Attributes is an ActiveRecord extension that prevents existing data from changing"
|
|
8
|
+
end
|
|
9
|
+
|
data/init.rb
ADDED
data/install.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
puts IO.read(File.join(File.dirname(__FILE__), 'README'))
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'activerecord'
|
|
3
|
+
|
|
4
|
+
module ImmutableErrors
|
|
5
|
+
class ImmutableAttributeError < ActiveRecord::ActiveRecordError
|
|
6
|
+
end
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
module ImmutableAttributes
|
|
10
|
+
VERSION = "1.0.3"
|
|
11
|
+
def attr_immutable(*args)
|
|
12
|
+
args.each do |meth|
|
|
13
|
+
class_eval do
|
|
14
|
+
define_method("#{meth}=") do |value|
|
|
15
|
+
new_record? || read_attribute(meth).nil? ?
|
|
16
|
+
write_attribute(meth, value) :
|
|
17
|
+
raise(ActiveRecord::ImmutableAttributeError, "#{meth} is immutable!")
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def validates_immutable(*attr_names)
|
|
24
|
+
config = { :on => :update, :if => lambda {|x| true}, :message => "can't be changed" }
|
|
25
|
+
config.update(attr_names.extract_options!)
|
|
26
|
+
|
|
27
|
+
@immutables = attr_names
|
|
28
|
+
|
|
29
|
+
attr_names.each do |meth|
|
|
30
|
+
class_eval do
|
|
31
|
+
define_method("original_#{meth}") do
|
|
32
|
+
instance_variable_get("@original_#{meth}")
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
class_eval do
|
|
38
|
+
def self.immutables
|
|
39
|
+
@immutables
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def after_initialize; end;
|
|
43
|
+
|
|
44
|
+
def setup_originals
|
|
45
|
+
self.class.immutables.each do |attr_name|
|
|
46
|
+
instance_variable_set("@original_#{attr_name}", send(attr_name.to_s))
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
after_initialize :setup_originals
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
validates_each(attr_names, config) do |record, attr_name, value|
|
|
54
|
+
next if record.send("original_#{attr_name.to_s}").nil?
|
|
55
|
+
record.errors.add(attr_name, config[:message]) if record.send("original_#{attr_name.to_s}") != record.send(attr_name.to_s)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
ActiveRecord.send :include, ImmutableErrors
|
|
61
|
+
ActiveRecord::Base.extend ImmutableAttributes
|
data/rails/init.rb
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require 'test/unit'
|
|
2
|
+
require 'rubygems'
|
|
3
|
+
require 'activerecord'
|
|
4
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'immutable_attributes')
|
|
5
|
+
|
|
6
|
+
ActiveRecord::Base.establish_connection(
|
|
7
|
+
:adapter => "sqlite3",
|
|
8
|
+
:dbfile => ":memory:"
|
|
9
|
+
)
|
|
10
|
+
ActiveRecord::Schema.define do
|
|
11
|
+
create_table :records do |table|
|
|
12
|
+
table.column :name, :string
|
|
13
|
+
table.column :body, :string
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
class Record < ActiveRecord::Base
|
|
18
|
+
attr_immutable :name
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
class ImmutableAttributesTest < Test::Unit::TestCase
|
|
22
|
+
|
|
23
|
+
def test_immutable_attribute_can_be_set
|
|
24
|
+
assert Record.new(:name => 'record name')
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def test_immutable_attribute_cannot_be_changed
|
|
28
|
+
record = Record.create!(:name => 'record name')
|
|
29
|
+
assert_raises(ActiveRecord::ImmutableAttributeError) { record.update_attributes(:name => 'new name') }
|
|
30
|
+
end
|
|
31
|
+
end
|
data/uninstall.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: IPGlider-ImmutableAttributes
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.3.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Jack Danger Canty
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2008-12-16 00:00:00 -08:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies:
|
|
15
|
+
- !ruby/object:Gem::Dependency
|
|
16
|
+
name: hoe
|
|
17
|
+
type: :development
|
|
18
|
+
version_requirement:
|
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
20
|
+
requirements:
|
|
21
|
+
- - ">="
|
|
22
|
+
- !ruby/object:Gem::Version
|
|
23
|
+
version: 1.8.2
|
|
24
|
+
version:
|
|
25
|
+
description:
|
|
26
|
+
email:
|
|
27
|
+
- git@6brand.com
|
|
28
|
+
executables: []
|
|
29
|
+
|
|
30
|
+
extensions: []
|
|
31
|
+
|
|
32
|
+
extra_rdoc_files:
|
|
33
|
+
- Manifest.txt
|
|
34
|
+
files:
|
|
35
|
+
- Manifest.txt
|
|
36
|
+
- README
|
|
37
|
+
- Rakefile
|
|
38
|
+
- init.rb
|
|
39
|
+
- install.rb
|
|
40
|
+
- lib/immutable_attributes.rb
|
|
41
|
+
- rails/init.rb
|
|
42
|
+
- test/immutable_attributes_test.rb
|
|
43
|
+
- uninstall.rb
|
|
44
|
+
has_rdoc: true
|
|
45
|
+
homepage:
|
|
46
|
+
post_install_message:
|
|
47
|
+
rdoc_options:
|
|
48
|
+
- --main
|
|
49
|
+
- README.txt
|
|
50
|
+
require_paths:
|
|
51
|
+
- lib
|
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
53
|
+
requirements:
|
|
54
|
+
- - ">="
|
|
55
|
+
- !ruby/object:Gem::Version
|
|
56
|
+
version: "0"
|
|
57
|
+
version:
|
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
|
+
requirements:
|
|
60
|
+
- - ">="
|
|
61
|
+
- !ruby/object:Gem::Version
|
|
62
|
+
version: "0"
|
|
63
|
+
version:
|
|
64
|
+
requirements: []
|
|
65
|
+
|
|
66
|
+
rubyforge_project: immutableattributes
|
|
67
|
+
rubygems_version: 1.2.0
|
|
68
|
+
signing_key:
|
|
69
|
+
specification_version: 2
|
|
70
|
+
summary: Immutable Attributes is an ActiveRecord extension that prevents existing data from changing
|
|
71
|
+
test_files: []
|
|
72
|
+
|