SpiritSC-case_collection_observer 0.1.0

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.
@@ -0,0 +1,2 @@
1
+ v0.1.0
2
+ Init project...
data/LICENSE ADDED
File without changes
@@ -0,0 +1,14 @@
1
+ CHANGELOG
2
+ LICENSE
3
+ Manifest
4
+ README
5
+ Rakefile
6
+ generators/case_observer/USAGE
7
+ generators/case_observer/case_observer_generator.rb
8
+ generators/case_observer/templates/case_collection_observer.rb
9
+ generators/case_observer/templates/case_collection_observer_test.rb
10
+ init.rb
11
+ lib/case_collection_observer.rb
12
+ lib/case_collection_observer/base.rb
13
+ lib/case_collection_observer/version.rb
14
+ lib/casecollectionobserver.rb
data/README ADDED
@@ -0,0 +1,139 @@
1
+ == Case Collection Observer v. 0.1.0
2
+ Simple notifier for object in HM and HABTM associations in AR...
3
+
4
+ <b>Usage:</b>
5
+
6
+ * Installation
7
+ sudo gem install case_collection_observer --source http://gems.github.com/
8
+
9
+ * Generator:
10
+
11
+ script/console case_observer OBSERVER_NAME MODELS_TO_OBSERVE [options]
12
+
13
+ # options:
14
+ --skip-test => Skip all tests for observer
15
+
16
+ <b>Rails example:</b>
17
+
18
+ - Simple construction with "has_many" association
19
+
20
+ # "Post" and "Comment" models
21
+
22
+ class Post < ActiveRecord::Base
23
+ has_many_with_observer :comments
24
+ end
25
+
26
+ class Comment < ActiveRecord::Base
27
+ belongs_to :post
28
+ end
29
+
30
+ # simple "Blog" observer for "Post" model
31
+
32
+ class BlogObserver < ActiveRecord::Observer
33
+ observe :post
34
+
35
+ def before_save_comments(comment)
36
+ end
37
+
38
+ def after_save_comments(comment)
39
+ end
40
+
41
+ def before_destroy_comments(comment)
42
+ end
43
+
44
+ def after_destroy_comments(comment)
45
+ end
46
+ end
47
+
48
+ - Simple construction with "has_many :through" association
49
+
50
+ # "Post", "Category" and "Categorization" models
51
+
52
+ class Post < ActiveRecord::Base
53
+ has_many :categorization
54
+ has_many_with_observer :categories, :through => :categorization
55
+ end
56
+
57
+ class Category < ActiveRecord::Base
58
+ has_many :categorization
59
+ has_many_with_observer :posts, :through => :categorization
60
+ end
61
+
62
+ class Categorization < ActiveRecord::Base
63
+ belongs_to :post
64
+ belongs_to :category
65
+ end
66
+
67
+ # simple "Blog" observer for "Post" and "Category" models
68
+
69
+ class BlogObserver < ActiveRecord::Observer
70
+ observe :post, :category
71
+
72
+ def before_save_posts(post)
73
+ end
74
+
75
+ def after_save_posts(post)
76
+ end
77
+
78
+ def before_destroy_posts(post)
79
+ end
80
+
81
+ def after_destroy_posts(post)
82
+ end
83
+
84
+ def before_save_categories(category)
85
+ end
86
+
87
+ def after_save_categories(category)
88
+ end
89
+
90
+ def before_destroy_categories(category)
91
+ end
92
+
93
+ def after_destroy_categories(category)
94
+ end
95
+ end
96
+
97
+ - Simple construction with "has_and_belongs_to_many" association
98
+
99
+ # "Post", "Category" models and "categories_posts" table
100
+
101
+ class Post < ActiveRecord::Base
102
+ has_and_belongs_to_many_with_observer :categories
103
+ end
104
+
105
+ class Category < ActiveRecord::Base
106
+ has_and_belongs_to_many_with_observer :posts
107
+ end
108
+
109
+ # simple "Blog" observer for "Post" and "Category" models
110
+
111
+ class BlogObserver < ActiveRecord::Observer
112
+ observe :post, :category
113
+
114
+ def before_save_posts(post)
115
+ end
116
+
117
+ def after_save_posts(post)
118
+ end
119
+
120
+ def before_destroy_posts(post)
121
+ end
122
+
123
+ def after_destroy_posts(post)
124
+ end
125
+
126
+ def before_save_categories(category)
127
+ end
128
+
129
+ def after_save_categories(category)
130
+ end
131
+
132
+ def before_destroy_categories(category)
133
+ end
134
+
135
+ def after_destroy_categories(category)
136
+ end
137
+ end
138
+
139
+ Copyright © 2009 Szymon Czarnecki
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('case_collection_observer', '0.1.0') do |p|
6
+ p.description = "Simple notifier for object in HM and HABTM associations in AR..."
7
+ p.url = "http://github.com/SpiritSC/Case-Collection-Observer"
8
+ p.author = "Szymon Czarnecki"
9
+ p.email = "spirit_sc@thinkdifferent.pl"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{case_collection_observer}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Szymon Czarnecki"]
9
+ s.date = %q{2009-09-21}
10
+ s.description = %q{Simple notifier for object in HM and HABTM associations in AR...}
11
+ s.email = %q{spirit_sc@thinkdifferent.pl}
12
+ s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README", "lib/case_collection_observer.rb", "lib/case_collection_observer/base.rb", "lib/case_collection_observer/version.rb", "lib/casecollectionobserver.rb"]
13
+ s.files = ["CHANGELOG", "LICENSE", "Manifest", "README", "Rakefile", "generators/case_observer/USAGE", "generators/case_observer/case_observer_generator.rb", "generators/case_observer/templates/case_collection_observer.rb", "generators/case_observer/templates/case_collection_observer_test.rb", "init.rb", "lib/case_collection_observer.rb", "lib/case_collection_observer/base.rb", "lib/case_collection_observer/version.rb", "lib/casecollectionobserver.rb", "case_collection_observer.gemspec"]
14
+ s.homepage = %q{http://github.com/SpiritSC/Case-Collection-Observer}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Case_collection_observer", "--main", "README"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{case_collection_observer}
18
+ s.rubygems_version = %q{1.3.5}
19
+ s.summary = %q{Simple notifier for object in HM and HABTM associations in AR...}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
26
+ else
27
+ end
28
+ else
29
+ end
30
+ end
File without changes
@@ -0,0 +1,60 @@
1
+ class CaseObserverGenerator < Rails::Generator::NamedBase
2
+ attr_accessor :observer
3
+ attr_accessor :models
4
+
5
+ def initialize(runtime_args, runtime_options = {})
6
+ super
7
+ @observer = class_name
8
+ @models = @args
9
+ end
10
+
11
+ def manifest
12
+ record do |m|
13
+ # Check existing folders
14
+ m.directory "app/models"
15
+ m.directory "test/unit"
16
+
17
+ # Copy observer
18
+ m.template "case_collection_observer.rb", "app/models/#{observer_singular}_observer.rb"
19
+
20
+ # Copy observer tests
21
+ unless options[:skip_test]
22
+ m.template "case_collection_observer_test.rb", "test/unit/#{observer_singular}_observer_test.rb"
23
+ end
24
+ end
25
+ end
26
+
27
+ def observer_singular
28
+ observer.underscore
29
+ end
30
+
31
+ def observer_singular_class
32
+ observer_singular.camelize
33
+ end
34
+
35
+ def models_singular
36
+ models.collect { |m| m.underscore }
37
+ end
38
+
39
+ def models_singular_sym
40
+ models_singular.collect { |m| ":#{m.to_sym}" }
41
+ end
42
+
43
+ protected
44
+ def banner
45
+ "\nCase Collection Observer usage:\n" +
46
+ "========================================================" +
47
+ "\n#{$0} case_observer OBSERVER_NAME MODELS_TO_OBSERVE [options]\n"
48
+ end
49
+
50
+ def add_options!(opt)
51
+ opt.separator ''
52
+ opt.separator 'Case Collection Observer options:'
53
+ opt.separator '========================================================'
54
+ opt.on("--skip-test", "Skip all tests for observer") { |value| options[:skip_test] = value }
55
+ end
56
+
57
+ def usage_message
58
+ ''
59
+ end
60
+ end
@@ -0,0 +1,18 @@
1
+ class <%= observer_singular_class %>Observer < ActiveRecord::Observer
2
+ <% unless models.empty? %>
3
+ observe <%= models_singular_sym.join(', ') %>
4
+ <% end %>
5
+ <% for model in models_singular %>
6
+ def before_save_<%= model.pluralize %>(<%= model %>)
7
+ end
8
+
9
+ def after_save_<%= model.pluralize %>(<%= model %>)
10
+ end
11
+
12
+ def before_destroy_<%= model.pluralize %>(<%= model %>)
13
+ end
14
+
15
+ def after_destroy_<%= model.pluralize %>(<%= model %>)
16
+ end
17
+ <% end %>
18
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class <%= observer_singular_class %>ObserverTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "case_collection_observer"
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/case_collection_observer/base"
@@ -0,0 +1,67 @@
1
+ module CaseCollectionObserver
2
+ module Base
3
+ def self.included(base)
4
+ base.class_eval do
5
+ include InstanceMethods
6
+ extend ClassMethods
7
+ end
8
+ end
9
+
10
+ module InstanceMethods
11
+ end
12
+
13
+ module ClassMethods
14
+ def has_many_with_observer(association_id, options = {}, &extension)
15
+ has_many association_id, case_observer_options(association_id, options), &extension
16
+ case_observer_callbacks(association_id)
17
+ end
18
+
19
+ def has_and_belongs_to_many_with_observer(association_id, options = {}, &extension)
20
+ has_and_belongs_to_many association_id, case_observer_options(association_id, options), &extension
21
+ case_observer_callbacks(association_id)
22
+ end
23
+
24
+ private
25
+ def case_observer_callbacks(association_id)
26
+ ["before_save_#{association_id}", "after_save_#{association_id}",
27
+ "before_destroy_#{association_id}", "after_destroy_#{association_id}"].each do |method|
28
+ define_method method do |record|
29
+ self.class.changed
30
+ self.class.notify_observers(method, record)
31
+ end
32
+ end
33
+ end
34
+
35
+ def case_observer_options(association_id, options = {})
36
+ callbacks = %w(before_add after_add before_remove after_remove)
37
+ callbacks.each do |callback_name|
38
+ new_callback = case callback_name.to_sym
39
+ when :before_add
40
+ "before_save_#{association_id}".to_sym
41
+ when :after_add
42
+ "after_save_#{association_id}".to_sym
43
+ when :before_remove
44
+ "before_destroy_#{association_id}".to_sym
45
+ when :after_remove
46
+ "after_destroy_#{association_id}".to_sym
47
+ end
48
+ if options[callback_name.to_sym]
49
+ defined_callbacks = options[callback_name.to_sym]
50
+ options[callback_name.to_sym] = [defined_callbacks, new_callback]
51
+ else
52
+ options[callback_name.to_sym] = new_callback
53
+ end
54
+ end
55
+ return options
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ if defined?(::ActiveRecord)
62
+ module ::ActiveRecord
63
+ class Base
64
+ include CaseCollectionObserver::Base
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,9 @@
1
+ module CaseCollectionObserver
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ TINY = 0
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
@@ -0,0 +1 @@
1
+ require "case_collection_observer"
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: SpiritSC-case_collection_observer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Szymon Czarnecki
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-21 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Simple notifier for object in HM and HABTM associations in AR...
17
+ email: spirit_sc@thinkdifferent.pl
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - CHANGELOG
24
+ - LICENSE
25
+ - README
26
+ - lib/case_collection_observer.rb
27
+ - lib/case_collection_observer/base.rb
28
+ - lib/case_collection_observer/version.rb
29
+ - lib/casecollectionobserver.rb
30
+ files:
31
+ - CHANGELOG
32
+ - LICENSE
33
+ - Manifest
34
+ - README
35
+ - Rakefile
36
+ - generators/case_observer/USAGE
37
+ - generators/case_observer/case_observer_generator.rb
38
+ - generators/case_observer/templates/case_collection_observer.rb
39
+ - generators/case_observer/templates/case_collection_observer_test.rb
40
+ - init.rb
41
+ - lib/case_collection_observer.rb
42
+ - lib/case_collection_observer/base.rb
43
+ - lib/case_collection_observer/version.rb
44
+ - lib/casecollectionobserver.rb
45
+ - case_collection_observer.gemspec
46
+ has_rdoc: false
47
+ homepage: http://github.com/SpiritSC/Case-Collection-Observer
48
+ licenses:
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --line-numbers
52
+ - --inline-source
53
+ - --title
54
+ - Case_collection_observer
55
+ - --main
56
+ - README
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "1.2"
70
+ version:
71
+ requirements: []
72
+
73
+ rubyforge_project: case_collection_observer
74
+ rubygems_version: 1.3.5
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Simple notifier for object in HM and HABTM associations in AR...
78
+ test_files: []
79
+