acts-as-savable 0.0.1

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
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --backtrace
3
+ --format doc
data/Changelog.md ADDED
@@ -0,0 +1 @@
1
+
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem "activerecord", "3.2.3"
6
+ gem "sqlite3", "1.3.6"
7
+ gem "rspec", "~> 2.9.0"
8
+ gem "database_cleaner", "0.7.2"
9
+ gem "guard-rspec", "0.7.0"
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :cli => '--format doc' do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { "spec" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
data/History.txt ADDED
@@ -0,0 +1,3 @@
1
+ ### 0.0.1 / 2012-04-25
2
+
3
+ Initial release
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 David Chelimsky
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,41 @@
1
+ # ActsAsSavable
2
+
3
+ acts-as-savable extends an ActiveRecord model so that it can read from a view,
4
+ and save to a table.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'acts-as-savable'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install acts-as-savable
19
+
20
+ ## Usage
21
+
22
+ Given tables named "things" and "thing_decorations", and a view named
23
+ "decorated_things" that joins on the other two, you can declare a
24
+ `DecoratedThing` class like this:
25
+
26
+ class DecoratedThing < ActiveRecord::Base
27
+ saves_to :thing_decorations, :thing_id, :decoration
28
+ end
29
+
30
+ All the finders work as expected, but `save`, `save!`, `update_attribute`,
31
+ `update_attributes`, and `update_attributes!` all create and/or update records
32
+ in the "thing_decorations" table (in this case updating ony the `:thing_id` and
33
+ `:decoration` columns).
34
+
35
+ ## Status
36
+
37
+ This was extracted from an app, and is constrained by assumptions related to
38
+ that app. It is very naive and not likely to solve all of your problems. If you
39
+ have a problem that this almost solves, but not quite, please file an issue at
40
+ https://github.com/dchelimsky/acts-as-savable/issues and help me to make it
41
+ more useful for a wider audience.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/acts-as-savable/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["David Chelimsky"]
6
+ gem.email = ["dchelimsky@gmail.com"]
7
+ gem.description = %q{ActiveRecord plugin for a view-backed model. Stores data to an underlying table.}
8
+ gem.summary = %q{ActiveRecord plugin for a view-backed model.}
9
+ gem.homepage = "http://github.com/dchelimsky/acts-as-savable"
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 = "acts-as-savable"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = ActsAsSavable::VERSION
17
+ end
@@ -0,0 +1,32 @@
1
+ require "acts-as-savable/version"
2
+
3
+ module ActsAsSavable
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ def saves_to(table_name, *attrs)
8
+ class_eval do
9
+ def save(*args)
10
+ _acts_as_savable_object.save(*args)
11
+ end
12
+
13
+ def save!(*args)
14
+ _acts_as_savable_object.save!(*args)
15
+ end
16
+
17
+ private
18
+
19
+ define_method :_acts_as_savable_object do
20
+ klass = Class.new(ActiveRecord::Base) { self.table_name = table_name }
21
+ instance = id ? klass.find(id) : klass.new
22
+ attrs.each {|attr| instance.send("#{attr}=", send(attr))}
23
+ instance
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ ActiveRecord::Base.class_eval do
31
+ include ActsAsSavable
32
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsSavable
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+
3
+ ActiveRecord::Base.connection.instance_eval do
4
+ create_table :things do |t|
5
+ t.string :name
6
+ end
7
+
8
+ create_table :thing_decorations do |t|
9
+ t.integer :thing_id
10
+ t.string :decoration
11
+ end
12
+
13
+ execute <<-SQL
14
+ create view view_backed_things as
15
+ select
16
+ thing_decorations.id,
17
+ things.id thing_id,
18
+ things.name,
19
+ thing_decorations.decoration
20
+ from things
21
+ left outer join thing_decorations on thing_decorations.thing_id = things.id;
22
+ SQL
23
+ end
24
+
25
+ class Thing < ActiveRecord::Base; end
26
+ class ThingDecoration < ActiveRecord::Base; end
27
+ class ViewBackedThing < ActiveRecord::Base
28
+ saves_to :thing_decorations, :thing_id, :decoration
29
+ end
30
+
31
+ describe ActsAsSavable do
32
+ shared_examples "examples" do |group_description|
33
+ metadata[:example_group][:description] = group_description
34
+
35
+ it "using save" do
36
+ view_backed_thing = ViewBackedThing.first
37
+ view_backed_thing.decoration = "curtain"
38
+ view_backed_thing.save
39
+ ViewBackedThing.first.decoration.should eq("curtain")
40
+ end
41
+
42
+ it "using save!" do
43
+ view_backed_thing = ViewBackedThing.first
44
+ view_backed_thing.decoration = "curtain"
45
+ view_backed_thing.save!
46
+ ViewBackedThing.first.decoration.should eq("curtain")
47
+ end
48
+
49
+ it "using update_attribute" do
50
+ view_backed_thing = ViewBackedThing.first
51
+ view_backed_thing.update_attribute(:decoration, "curtain")
52
+ ViewBackedThing.first.decoration.should eq("curtain")
53
+ end
54
+
55
+ it "using update_attributes" do
56
+ view_backed_thing = ViewBackedThing.first
57
+ view_backed_thing.update_attributes(:decoration => "curtain")
58
+ ViewBackedThing.first.decoration.should eq("curtain")
59
+ end
60
+
61
+ it "using update_attributes!" do
62
+ view_backed_thing = ViewBackedThing.first
63
+ view_backed_thing.update_attributes!(:decoration => "curtain")
64
+ ViewBackedThing.first.decoration.should eq("curtain")
65
+ end
66
+ end
67
+
68
+ include_examples "examples", "creates a new record" do
69
+ before(:each) do
70
+ Thing.create!(:name => "Something")
71
+ end
72
+ end
73
+
74
+ include_examples "examples", "updates an existing record" do
75
+ before(:each) do
76
+ thing = Thing.create!(:name => "Something")
77
+ ThingDecoration.create!(
78
+ :thing_id => thing.id,
79
+ :decoration => nil
80
+ )
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,16 @@
1
+ require 'active_record'
2
+ require 'acts-as-savable'
3
+ require 'database_cleaner'
4
+
5
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
6
+ DatabaseCleaner.strategy = :transaction
7
+
8
+ RSpec.configure do |config|
9
+ config.treat_symbols_as_metadata_keys_with_true_values = true
10
+ config.run_all_when_everything_filtered = true
11
+ config.filter_run :focus
12
+ config.alias_it_should_behave_like_to :include_examples
13
+
14
+ config.before(:each) { DatabaseCleaner.start }
15
+ config.after( :each) { DatabaseCleaner.clean }
16
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts-as-savable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Chelimsky
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-25 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ActiveRecord plugin for a view-backed model. Stores data to an underlying
15
+ table.
16
+ email:
17
+ - dchelimsky@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - .rspec
24
+ - Changelog.md
25
+ - Gemfile
26
+ - Guardfile
27
+ - History.txt
28
+ - LICENSE
29
+ - README.md
30
+ - Rakefile
31
+ - acts-as-savable.gemspec
32
+ - lib/acts-as-savable.rb
33
+ - lib/acts-as-savable/version.rb
34
+ - spec/acts-as-savable_spec.rb
35
+ - spec/spec_helper.rb
36
+ homepage: http://github.com/dchelimsky/acts-as-savable
37
+ licenses: []
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 1.8.10
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: ActiveRecord plugin for a view-backed model.
60
+ test_files:
61
+ - spec/acts-as-savable_spec.rb
62
+ - spec/spec_helper.rb