activerecord-readonly_model 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ test.sqlite3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-readonly_model.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 TODO: Write your name
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,59 @@
1
+ # ActiveRecord::ReadonlyModel
2
+
3
+ `ActiveRecord:ReadonlyModel` does what you'd think. It makes any
4
+ ActiveRecord model read only. Any attempt to persist changes to a
5
+ read-only model will result in an `ActiveRecord::ReadOnlyRecord`
6
+ exception.
7
+
8
+ Read-only models are useful when you're storing 'static' kind of data
9
+ like descriptive types or statuses that don't change while your application is
10
+ executing.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ gem 'activerecord-readonly_model'
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install activerecord-readonly_model
25
+
26
+ ## Usage
27
+
28
+ To use, just include the `ActiveRecord::ReadonlyModel` module in your
29
+ model:
30
+
31
+ class ThingStatus < ActiveRecord::Base
32
+ include ActiveRecord::ReadonlyModel
33
+ end
34
+
35
+ Any attempt to update or destroy an instance of `ThingStatus` will raise an
36
+ `ActiveRecord::ReadOnlyRecord` exception.
37
+
38
+ ## Bypass
39
+
40
+ Of course sometimes you'll want to bypass the read-only restriction
41
+ (like when seeding your database). To temporarily bypass, make
42
+ updates/inserts/deletes in a bypass block:
43
+
44
+ ActiveRecord::ReadonlyModel.bypass do
45
+ ThingStatus.create(:name => 'Active', :description => 'An active thing')
46
+ end
47
+
48
+ ## Limitations
49
+
50
+ This gem will only prevent creates, saves, and destroys. `.delete()` and
51
+ `.delete_all()` behavior is unaffected by this gem!
52
+
53
+ ## Contributing
54
+
55
+ 1. Fork it
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new(:test) do |test|
6
+ test.libs << 'lib' << 'test'
7
+ test.pattern = 'test/**/test_*.rb'
8
+ test.verbose = true
9
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/active_record-readonly_model/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Christian Dinger"]
6
+ gem.email = ["cdinger@gmail.com"]
7
+ gem.description = %q{Makes an ActiveRecord model read-only}
8
+ gem.summary = %q{Makes an ActiveRecord model read-only}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "activerecord-readonly_model"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = ActiveRecord::ReadonlyModel::VERSION
17
+
18
+ gem.add_dependency('activerecord', '>= 3.0.0')
19
+ end
@@ -0,0 +1,29 @@
1
+ require 'active_record'
2
+ require 'active_record-readonly_model/version'
3
+
4
+ module ActiveRecord
5
+ module ReadonlyModel
6
+ @@bypass_flag = false
7
+
8
+ def self.bypass(&block)
9
+ @@bypass_flag = true
10
+ yield
11
+ @@bypass_flag = false
12
+ end
13
+
14
+ # Prevents objects from being created
15
+ def readonly?
16
+ super && !@@bypass_flag
17
+ end
18
+
19
+ def self.included(base)
20
+ base.class_eval do
21
+ def readonly
22
+ true
23
+ end
24
+ before_save { raise ActiveRecord::ReadOnlyRecord unless @@bypass_flag }
25
+ before_destroy { raise ActiveRecord::ReadOnlyRecord unless @@bypass_flag }
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveRecord
2
+ module ReadonlyModel
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'active_record'
5
+ require 'active_record-readonly_model'
6
+
7
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => 'test.sqlite3')
@@ -0,0 +1,49 @@
1
+ require 'helper'
2
+
3
+ class TestReadonly < Test::Unit::TestCase
4
+ context "Some static model" do
5
+ setup do
6
+ ActiveRecord::Base.connection.create_table :static_things, :force => true do |t|
7
+ t.string :name
8
+ end
9
+ ActiveRecord::Base.connection.execute("insert into static_things (name) values ('Blah thing')")
10
+ class StaticThing < ActiveRecord::Base
11
+ include ActiveRecord::ReadonlyModel
12
+ end
13
+ end
14
+
15
+ teardown do
16
+ ActiveRecord::Base.connection.drop_table :static_things
17
+ end
18
+
19
+ should "allow destroy in an ignore_readonly block" do
20
+ before_count = StaticThing.count
21
+ assert_nothing_raised do
22
+ ActiveRecord::ReadonlyModel.bypass do
23
+ thing = StaticThing.first
24
+ assert(thing.destroy)
25
+ end
26
+ end
27
+ assert_not_equal(before_count, StaticThing.count)
28
+ end
29
+
30
+ should "allow delete in an ignore_readonly block" do
31
+ before_count = StaticThing.count
32
+ assert_nothing_raised do
33
+ #StaticThing.ignore_readonly { StaticThing.delete_all }
34
+ ActiveRecord::ReadonlyModel.bypass { StaticThing.delete_all }
35
+ end
36
+ assert_not_equal(before_count, StaticThing.count)
37
+ end
38
+
39
+ should "allow update in an ignore_readonly block" do
40
+ assert_nothing_raised do
41
+ ActiveRecord::ReadonlyModel.bypass do
42
+ thing = StaticThing.first
43
+ thing.name = 'something different'
44
+ assert(thing.save)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,35 @@
1
+ require 'helper'
2
+
3
+ class TestReadonly < Test::Unit::TestCase
4
+ context "Some static model" do
5
+ setup do
6
+ ActiveRecord::Base.connection.create_table :static_things, :force => true do |t|
7
+ t.string :name
8
+ end
9
+ ActiveRecord::Base.connection.execute("insert into static_things (name) values ('Blah thing')")
10
+ class StaticThing < ActiveRecord::Base
11
+ include ActiveRecord::ReadonlyModel
12
+ end
13
+ end
14
+
15
+ teardown do
16
+ ActiveRecord::Base.connection.drop_table :static_things
17
+ end
18
+
19
+ should "prevent destroy" do
20
+ before_count = StaticThing.count
21
+ assert_raise ActiveRecord::ReadOnlyRecord do
22
+ StaticThing.first.destroy
23
+ end
24
+ assert_equal(before_count, StaticThing.count)
25
+ end
26
+
27
+ should "prevent update" do
28
+ thing = StaticThing.first
29
+ thing.name = 'something different'
30
+ assert_raise ActiveRecord::ReadOnlyRecord do
31
+ thing.save
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestReadonly < Test::Unit::TestCase
4
+ should "have a version" do
5
+ assert_not_nil(ActiveRecord::ReadonlyModel::VERSION)
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-readonly_model
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Christian Dinger
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-06 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: 3.0.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.0
30
+ description: Makes an ActiveRecord model read-only
31
+ email:
32
+ - cdinger@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE
40
+ - README.md
41
+ - Rakefile
42
+ - activerecord-readonly_model.gemspec
43
+ - lib/active_record-readonly_model.rb
44
+ - lib/active_record-readonly_model/version.rb
45
+ - test/helper.rb
46
+ - test/test_ignore_readonly.rb
47
+ - test/test_readonly.rb
48
+ - test/test_version.rb
49
+ homepage: ''
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 1.8.24
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Makes an ActiveRecord model read-only
73
+ test_files:
74
+ - test/helper.rb
75
+ - test/test_ignore_readonly.rb
76
+ - test/test_readonly.rb
77
+ - test/test_version.rb