live_resource-activerecord 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,17 @@
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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ live_resource-activerecord
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-1.9.3-p392
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in live_resource-activerecord.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Will Madden
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,79 @@
1
+ # LiveResource::ActiveRecord
2
+
3
+ This gem provides the ActiveRecord LiveResource dependency.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'live_resource-activerecord'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install live_resource-activerecord
18
+
19
+ ## Configuration
20
+
21
+ You need to add this to the list of supported dependency types in your application config.
22
+
23
+ ```ruby
24
+ # config/application.rb (or development.rb, test.rb etc)
25
+
26
+ class Application < Rails::Application
27
+ ...
28
+ config.live_resource = {
29
+ dependency_types: [LiveResource::ActiveRecord::Dependency]
30
+ }
31
+ end
32
+ ```
33
+
34
+ ## Usage
35
+
36
+ ```ruby
37
+ # app/controllers/profiles_controller.rb
38
+
39
+ class ProfilesController < ApplicationController
40
+ ...
41
+
42
+ # show.json
43
+ # {
44
+ # name: @profile.name,
45
+ # avatar: {
46
+ # alt_text: @profile.avatar.alt_text,
47
+ # url: avatar_url( @profile.avatar )
48
+ # }
49
+ # }
50
+ def show
51
+ ...
52
+ end
53
+
54
+ live_resource :show do
55
+ identifier { |profile| profile_path(profile) }
56
+
57
+ # When a Profile instance is changed
58
+ depends_on(Profile) do |profile|
59
+ # Push an update for the resource belonging to the profile
60
+ push(profile)
61
+ end
62
+
63
+ # Since the view will change if the avatar changes, depend on that too
64
+ depends_on(Avatar) do |avatar|
65
+ push avatar.profile
66
+ end
67
+ end
68
+
69
+ ...
70
+ end
71
+ ```
72
+
73
+ ## Contributing
74
+
75
+ 1. Fork it
76
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
77
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
78
+ 4. Push to the branch (`git push origin my-new-feature`)
79
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,46 @@
1
+ require "live_resource/dependency"
2
+ require "active_record"
3
+
4
+ module LiveResource
5
+ module ActiveRecord
6
+
7
+ class Dependency < LiveResource::Dependency
8
+ DEFAULT_EVENTS = [:after_destroy, :after_save]
9
+
10
+ def self.accepts_target?(target)
11
+ !!(target < ::ActiveRecord::Base) # The < operator returns nil instead of false
12
+ end
13
+
14
+ attr_reader :resource, :model_class
15
+
16
+ def initialize(resource, model_class, proc, *events)
17
+ @model_class = model_class
18
+
19
+ if !events.empty?
20
+ @events = events
21
+ else
22
+ @events = DEFAULT_EVENTS
23
+ end
24
+
25
+ super(resource, model_class, proc)
26
+ end
27
+
28
+ def watch
29
+ @events.each { |event| observe event }
30
+ end
31
+
32
+ def observe(event)
33
+ dependency = self
34
+ model_class.class_eval do
35
+ send(event, dependency)
36
+ end
37
+ end
38
+
39
+ ::ActiveRecord::Callbacks::CALLBACKS.each do |callback|
40
+ event = callback
41
+ define_method(callback) { |record| invoke(record, event) }
42
+ end
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,5 @@
1
+ module LiveResource
2
+ module ActiveRecord
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'live_resource/activerecord/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "live_resource-activerecord"
8
+ spec.version = LiveResource::ActiveRecord::VERSION
9
+ spec.authors = ["Will Madden"]
10
+ spec.email = ["will@letsgeddit.com"]
11
+ spec.description = %q{Adds support to LiveRecord for ActiveRecord dependencies}
12
+ spec.summary = %q{Adds support to LiveRecord for ActiveRecord dependencies}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+
25
+ spec.add_dependency 'live_resource'
26
+ spec.add_dependency 'activerecord'
27
+
28
+ spec.add_development_dependency "rspec"
29
+ spec.add_development_dependency "sqlite3"
30
+ end
@@ -0,0 +1,102 @@
1
+ require "spec_helper"
2
+ require 'live_resource/resource'
3
+
4
+ include LiveResource::ActiveRecord
5
+
6
+ describe Dependency do
7
+
8
+ let(:dependency) { Dependency.new(resource, model_class, proc, *events) }
9
+
10
+ let(:resource) { double(LiveResource::Resource) }
11
+
12
+ let(:model_class) do
13
+ class User < ActiveRecord::Base
14
+ end
15
+ User
16
+ end
17
+
18
+ let(:events) { nil }
19
+ let(:proc) { double(Proc) }
20
+
21
+ ActiveRecord::Callbacks::CALLBACKS.each do |callback|
22
+ context "##{callback}" do
23
+ subject { dependency.send(callback.to_sym, record) }
24
+
25
+ let(:record) { model_class.new }
26
+
27
+ it "should call #invoke with the ActiveRecord instance and the callback name" do
28
+ dependency.should_receive(:invoke).with(record, callback)
29
+ subject
30
+ end
31
+ end
32
+ end
33
+
34
+ it "should pass the target to the superclass" do
35
+ expect(dependency.target).to be model_class
36
+ end
37
+
38
+ it "should pass the resource to the superclass" do
39
+ expect(dependency.resource).to be resource
40
+ end
41
+
42
+ describe ".accepts_target?" do
43
+ subject { Dependency.accepts_target?(target) }
44
+
45
+ context 'when the target is an ActiveRecord subclass' do
46
+ let(:user_class) do
47
+ class User < ActiveRecord::Base
48
+ end
49
+ User
50
+ end
51
+
52
+ let(:target) { user_class }
53
+
54
+ it { should == true }
55
+ end
56
+
57
+ context 'when the target is not an ActiveRecord subclass' do
58
+ let(:target) { Class.new }
59
+
60
+ it { should == false }
61
+ end
62
+ end
63
+
64
+ describe "#watch" do
65
+ subject { dependency.watch }
66
+
67
+ context 'when some events are supplied' do
68
+ let(:events) { [:after_create, :after_destroy] }
69
+
70
+ it 'should observe each supplied event' do
71
+ events.each do |event|
72
+ dependency.should_receive(:observe).with(event).ordered
73
+ end
74
+ subject
75
+ end
76
+ end
77
+
78
+ context 'when no events are supplied' do
79
+ it 'should observe each of the default events' do
80
+ Dependency::DEFAULT_EVENTS.each do |event|
81
+ dependency.should_receive(:observe).with(event).ordered
82
+ end
83
+ subject
84
+ end
85
+ end
86
+ end
87
+
88
+ describe "#observe" do
89
+ subject { dependency.observe(event) }
90
+
91
+ let(:event) { :after_create }
92
+
93
+ before do
94
+ model_class.stub(event)
95
+ end
96
+
97
+ it "should register itself for callbacks" do
98
+ model_class.should_receive(event).with(dependency)
99
+ subject
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe LiveResource::ActiveRecord do
4
+ it 'should have a version number' do
5
+ LiveResource::ActiveRecord::VERSION.should_not be_nil
6
+ end
7
+ end
data/spec/schema.rb ADDED
@@ -0,0 +1,7 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+
4
+ create_table :users, :force => true do |t|
5
+ t.string :name
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'active_record'
4
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3",
5
+ :database => File.dirname(__FILE__) + "/test.sqlite3")
6
+
7
+ require 'schema'
8
+
9
+ def require_tree(path)
10
+ path = File.expand_path(path, File.dirname(__FILE__))
11
+
12
+ files = Dir.glob(File.join(path, '**/*.rb'))
13
+ raise "Directory '#{path}' is empty" unless files.length > 0
14
+
15
+ files.each { |file| puts " require #{file}"; require file }
16
+ end
17
+
18
+ require_tree '../lib'
metadata ADDED
@@ -0,0 +1,178 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: live_resource-activerecord
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Will Madden
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
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: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
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: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: live_resource
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: activerecord
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rspec
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
+ - !ruby/object:Gem::Dependency
111
+ name: sqlite3
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description: Adds support to LiveRecord for ActiveRecord dependencies
127
+ email:
128
+ - will@letsgeddit.com
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - .gitignore
134
+ - .rspec
135
+ - .ruby-gemset
136
+ - .ruby-version
137
+ - .travis.yml
138
+ - Gemfile
139
+ - LICENSE.txt
140
+ - README.md
141
+ - Rakefile
142
+ - lib/live_resource/activerecord/dependency.rb
143
+ - lib/live_resource/activerecord/version.rb
144
+ - live_resource-activerecord.gemspec
145
+ - spec/live_resource/activerecord/dependency_spec.rb
146
+ - spec/live_resource/activerecord_spec.rb
147
+ - spec/schema.rb
148
+ - spec/spec_helper.rb
149
+ homepage: ''
150
+ licenses:
151
+ - MIT
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ none: false
158
+ requirements:
159
+ - - ! '>='
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ none: false
164
+ requirements:
165
+ - - ! '>='
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ requirements: []
169
+ rubyforge_project:
170
+ rubygems_version: 1.8.25
171
+ signing_key:
172
+ specification_version: 3
173
+ summary: Adds support to LiveRecord for ActiveRecord dependencies
174
+ test_files:
175
+ - spec/live_resource/activerecord/dependency_spec.rb
176
+ - spec/live_resource/activerecord_spec.rb
177
+ - spec/schema.rb
178
+ - spec/spec_helper.rb