acts_as_status_for 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+ gem 'rails'
3
+ gem 'sqlite3'
4
+ group :development do
5
+ gem "rspec", "~> 2.3.0"
6
+ gem "bundler", "~> 1.0.0"
7
+ gem "jeweler", "~> 1.6.1"
8
+ gem "rcov", ">= 0"
9
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,92 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionmailer (3.0.7)
6
+ actionpack (= 3.0.7)
7
+ mail (~> 2.2.15)
8
+ actionpack (3.0.7)
9
+ activemodel (= 3.0.7)
10
+ activesupport (= 3.0.7)
11
+ builder (~> 2.1.2)
12
+ erubis (~> 2.6.6)
13
+ i18n (~> 0.5.0)
14
+ rack (~> 1.2.1)
15
+ rack-mount (~> 0.6.14)
16
+ rack-test (~> 0.5.7)
17
+ tzinfo (~> 0.3.23)
18
+ activemodel (3.0.7)
19
+ activesupport (= 3.0.7)
20
+ builder (~> 2.1.2)
21
+ i18n (~> 0.5.0)
22
+ activerecord (3.0.7)
23
+ activemodel (= 3.0.7)
24
+ activesupport (= 3.0.7)
25
+ arel (~> 2.0.2)
26
+ tzinfo (~> 0.3.23)
27
+ activeresource (3.0.7)
28
+ activemodel (= 3.0.7)
29
+ activesupport (= 3.0.7)
30
+ activesupport (3.0.7)
31
+ arel (2.0.10)
32
+ builder (2.1.2)
33
+ diff-lcs (1.1.2)
34
+ erubis (2.6.6)
35
+ abstract (>= 1.0.0)
36
+ git (1.2.5)
37
+ i18n (0.5.0)
38
+ jeweler (1.6.2)
39
+ bundler (~> 1.0)
40
+ git (>= 1.2.5)
41
+ rake
42
+ mail (2.2.19)
43
+ activesupport (>= 2.3.6)
44
+ i18n (>= 0.4.0)
45
+ mime-types (~> 1.16)
46
+ treetop (~> 1.4.8)
47
+ mime-types (1.16)
48
+ polyglot (0.3.1)
49
+ rack (1.2.2)
50
+ rack-mount (0.6.14)
51
+ rack (>= 1.0.0)
52
+ rack-test (0.5.7)
53
+ rack (>= 1.0)
54
+ rails (3.0.7)
55
+ actionmailer (= 3.0.7)
56
+ actionpack (= 3.0.7)
57
+ activerecord (= 3.0.7)
58
+ activeresource (= 3.0.7)
59
+ activesupport (= 3.0.7)
60
+ bundler (~> 1.0)
61
+ railties (= 3.0.7)
62
+ railties (3.0.7)
63
+ actionpack (= 3.0.7)
64
+ activesupport (= 3.0.7)
65
+ rake (>= 0.8.7)
66
+ thor (~> 0.14.4)
67
+ rake (0.9.2)
68
+ rcov (0.9.9)
69
+ rspec (2.3.0)
70
+ rspec-core (~> 2.3.0)
71
+ rspec-expectations (~> 2.3.0)
72
+ rspec-mocks (~> 2.3.0)
73
+ rspec-core (2.3.1)
74
+ rspec-expectations (2.3.0)
75
+ diff-lcs (~> 1.1.2)
76
+ rspec-mocks (2.3.0)
77
+ sqlite3 (1.3.3)
78
+ thor (0.14.6)
79
+ treetop (1.4.9)
80
+ polyglot (>= 0.3.1)
81
+ tzinfo (0.3.28)
82
+
83
+ PLATFORMS
84
+ ruby
85
+
86
+ DEPENDENCIES
87
+ bundler (~> 1.0.0)
88
+ jeweler (~> 1.6.1)
89
+ rails
90
+ rcov
91
+ rspec (~> 2.3.0)
92
+ sqlite3
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Curtis Schofield
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,42 @@
1
+ ActiveRecord::Migration.create_table :things do |t|
2
+ t.datetime :on_hold_at
3
+ t.datetime :archived_at
4
+ t.datetime :featured_at
5
+ end
6
+
7
+ class Thing < ActiveRecord::Base
8
+ include ActsAsStatusFor
9
+ acts_as_status_for :on_hold, :archived, :featured do
10
+ scope :depends_on, not_on_hold.not_archived
11
+ end
12
+ end
13
+
14
+ ----
15
+ Given this code you will be granted the following abilities:
16
+
17
+ status
18
+ => returns a string '' with marks according to what status is set
19
+ status=('')
20
+ => enforces the status set to match the status string passed in
21
+ archived?, on_hold?, featured?
22
+ => check on status of flag
23
+ archived!, on_hold!, featured!
24
+ => turn on status & save
25
+ not_archived!, not_on_hold!, not_featured!
26
+ => turn off status & save
27
+
28
+ scopes :
29
+
30
+ not_archived, not_on_hold, not_featured,
31
+ archived , on_hold ' featured
32
+ --
33
+
34
+
35
+ please note : you can protect your code from failing to exectue when your migrations
36
+ have not run yet (like on staging) but the code referencies fields about to be added
37
+ via a migration - by the use of a block
38
+
39
+ In the above example the block contains a reference to 'not_on_hold' - this is a scope
40
+ which is created by the argument to acts_as_status. :on_hold must exist in the database
41
+ for this block to run - and actually - if anyone of the status marks _at database attribute
42
+ doens't exist - the code will not install itself properly
data/Rakefile ADDED
@@ -0,0 +1,52 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "acts_as_status_for"
18
+ gem.homepage = "http://github.com/robotarmy/acts_as_status_for"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Acts as Status For a list of events - did something occur? or not? with scoped finder?}
21
+ gem.description = %Q{Given a list of datetime _at attributes }
22
+ gem.email = "github.com@robotarmyma.de"
23
+ gem.authors = ["Curtis Schofield"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "acts_as_status_for #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
50
+
51
+ require './config/environment.rb'
52
+ ActsAsStatus::Application.load_tasks
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.1.1
@@ -0,0 +1,67 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{acts_as_status_for}
8
+ s.version = "1.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Curtis Schofield"]
12
+ s.date = %q{2011-06-14}
13
+ s.description = %q{Given a list of datetime _at attributes }
14
+ s.email = %q{github.com@robotarmyma.de}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "acts_as_status_for.gemspec",
29
+ "config/environment.rb",
30
+ "lib/acts_as_status_for.rb",
31
+ "spec/acts_as_status_for_spec.rb",
32
+ "spec/spec_helper.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/robotarmy/acts_as_status_for}
35
+ s.licenses = ["MIT"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.5.2}
38
+ s.summary = %q{Acts as Status For a list of events - did something occur? or not? with scoped finder?}
39
+
40
+ if s.respond_to? :specification_version then
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
+ s.add_runtime_dependency(%q<rails>, [">= 0"])
45
+ s.add_runtime_dependency(%q<sqlite3>, [">= 0"])
46
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
47
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
48
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.1"])
49
+ s.add_development_dependency(%q<rcov>, [">= 0"])
50
+ else
51
+ s.add_dependency(%q<rails>, [">= 0"])
52
+ s.add_dependency(%q<sqlite3>, [">= 0"])
53
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
54
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
55
+ s.add_dependency(%q<jeweler>, ["~> 1.6.1"])
56
+ s.add_dependency(%q<rcov>, [">= 0"])
57
+ end
58
+ else
59
+ s.add_dependency(%q<rails>, [">= 0"])
60
+ s.add_dependency(%q<sqlite3>, [">= 0"])
61
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
62
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
63
+ s.add_dependency(%q<jeweler>, ["~> 1.6.1"])
64
+ s.add_dependency(%q<rcov>, [">= 0"])
65
+ end
66
+ end
67
+
@@ -0,0 +1,12 @@
1
+ require 'rails/all'
2
+
3
+ module ActsAsStatus
4
+ class Application < Rails::Application
5
+ end
6
+ end
7
+
8
+ ActiveRecord::Base.establish_connection(
9
+ :adapter => 'sqlite3',
10
+ :database => ':memory:'
11
+ )
12
+
@@ -0,0 +1,98 @@
1
+ module ActsAsStatusFor
2
+ def self.included(base)
3
+ base.instance_eval do
4
+ include InstanceMethods
5
+ end
6
+ base.extend(ClassMethods)
7
+ end
8
+ module ClassMethods
9
+
10
+ def acts_as_status_for(*status_marks,&after_migrations)
11
+ @all_status_marks_exist = true
12
+ @on_at_events = status_marks
13
+ @off_at_events = on_at_events.collect do | event |
14
+ "not_#{event.to_s}".to_sym
15
+ end
16
+ install_scopes
17
+ install_methods
18
+ after_migrations.call(self) if @all_status_marks_exist && block_given?
19
+ end
20
+ def log_error(state)
21
+ STDERR.puts "Arel could not find #{state}_at in the database - skipping installation of acts_as_status"
22
+ end
23
+ def install_scopes
24
+ on_at_events.each do |state|
25
+ if self.arel_table["#{state}_at".to_sym] then
26
+ scope "#{state}".to_sym, where(self.arel_table["#{state}_at".to_sym].not_eq(nil))
27
+ scope "not_#{state}".to_sym, where(self.arel_table["#{state}_at".to_sym].eq(nil))
28
+ else
29
+ log_error(state)
30
+ @all_status_marks_exist = @all_status_marks_exist && false
31
+ end
32
+ end
33
+ end
34
+
35
+ def install_methods
36
+ on_at_events.each do |state|
37
+ if self.arel_table["#{state}_at".to_sym] then
38
+ define_method "#{state}?" do
39
+ !read_attribute("#{state}_at".to_sym).nil?
40
+ end
41
+
42
+ define_method "not_#{state}!" do
43
+ if self.__send__("#{state}?")
44
+ self.send("#{state}_at=".to_sym,nil)
45
+ self.save!
46
+ end
47
+ end
48
+
49
+ define_method "#{state}!" do
50
+ unless self.__send__("#{state}?")
51
+ self.send("#{state}_at=".to_sym,Time.now)
52
+ self.save!
53
+ end
54
+ end
55
+ else
56
+ log_error(state)
57
+ @all_status_marks_exist = @all_status_marks_exist && false
58
+ end
59
+ end
60
+ end
61
+
62
+ def off_at_events
63
+ @off_at_events
64
+ end
65
+ def all_at_events
66
+ @all_at_events ||= ( off_at_events | on_at_events )
67
+ end
68
+ def on_at_events
69
+ @on_at_events
70
+ end
71
+ end
72
+
73
+ module InstanceMethods
74
+
75
+ def status=(event_string)
76
+ case event_string
77
+ when ''
78
+ self.class.off_at_events.each do | event |
79
+ self.send("#{event}!") if self.respond_to?("#{event}!")
80
+ end
81
+ else
82
+ event_string.split(' ').each do | event |
83
+ if self.class.all_at_events.include?(event.to_sym)
84
+ self.send("#{event}!") if self.respond_to?("#{event}!")
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ def status
91
+ status = []
92
+ self.class.on_at_events.each do | event |
93
+ status << event.to_s if self.send("#{event}?")
94
+ end
95
+ status.join(' ')
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,98 @@
1
+ require 'spec_helper'
2
+
3
+ ActiveRecord::Migration.create_table :things do |t|
4
+ t.datetime :on_hold_at
5
+ t.datetime :archived_at
6
+ t.datetime :featured_at
7
+ end
8
+
9
+ class Thing < ActiveRecord::Base
10
+ include ActsAsStatusFor
11
+ end
12
+
13
+ describe ActsAsStatusFor do
14
+ subject {
15
+ Thing.new
16
+ }
17
+ context "for non-existing fields" do
18
+ before do
19
+ @ran = false
20
+ Thing.instance_eval do
21
+ acts_as_status_for :happing do
22
+ @ran = true
23
+ end
24
+ end
25
+ end
26
+ it "does not execute block" do
27
+ @ran.should be_false
28
+ end
29
+ end
30
+ context "install dependent helpers" do
31
+ before do
32
+ Thing.instance_eval do
33
+ acts_as_status_for :on_hold, :archived, :featured do
34
+ scope :depends_on, not_on_hold.not_archived
35
+ end
36
+ end
37
+ end
38
+ it "#depends_on" do
39
+ subject.class.respond_to?(:depends_on).should be_true
40
+ end
41
+ end
42
+
43
+ context "#status" do
44
+ before do
45
+ Thing.instance_eval do
46
+ acts_as_status_for :on_hold, :archived, :featured
47
+ end
48
+ end
49
+ it "defaults to ''" do
50
+ subject.status.should == ''
51
+ end
52
+
53
+ it "setting it to blank clears all states" do
54
+ subject.on_hold!
55
+ subject.archived!
56
+ subject.featured!
57
+ subject.status = ''
58
+ subject.status.should == ''
59
+ end
60
+ {
61
+ # class finder method
62
+ # => [ list of event states that belong to finder]
63
+ #
64
+ :featured => [:featured],
65
+ :on_hold => [:on_hold],
66
+ :archived => [:archived]
67
+ }.each do |scope,states|
68
+ states.each do |state|
69
+ it "can be used to set events" do
70
+ subject.status = state.to_s
71
+ subject.send(%%#{state}?%).should be_true
72
+ subject.status.should include(state.to_s)
73
+ end
74
+
75
+ it "can be reversed" do
76
+ subject.status = state.to_s
77
+ subject.send("#{state}?").should be_true
78
+ subject.status = "not_" + state.to_s
79
+ subject.send("#{state}?").should be_false
80
+ end
81
+
82
+ it "#{state} sets state string" do
83
+ subject.send("#{state}!")
84
+ subject.send("#{state}?").should be_true
85
+ subject.status.should include(state.to_s)
86
+ end
87
+
88
+ it "#{state} is in the scope #{scope}" do
89
+ subject.send("#{state}!")
90
+ subject.send("#{state}?").should be_true
91
+ subject.class.send(scope).should include(subject)
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'acts_as_status_for'
5
+ require './config/environment.rb'
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_status_for
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.1.1
6
+ platform: ruby
7
+ authors:
8
+ - Curtis Schofield
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-14 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rails
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: rspec
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.3.0
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: bundler
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ version: 1.0.0
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: jeweler
62
+ requirement: &id005 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ~>
66
+ - !ruby/object:Gem::Version
67
+ version: 1.6.1
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: *id005
71
+ - !ruby/object:Gem::Dependency
72
+ name: rcov
73
+ requirement: &id006 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ type: :development
80
+ prerelease: false
81
+ version_requirements: *id006
82
+ description: "Given a list of datetime _at attributes "
83
+ email: github.com@robotarmyma.de
84
+ executables: []
85
+
86
+ extensions: []
87
+
88
+ extra_rdoc_files:
89
+ - LICENSE.txt
90
+ - README
91
+ files:
92
+ - .document
93
+ - .rspec
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - LICENSE.txt
97
+ - README
98
+ - Rakefile
99
+ - VERSION
100
+ - acts_as_status_for.gemspec
101
+ - config/environment.rb
102
+ - lib/acts_as_status_for.rb
103
+ - spec/acts_as_status_for_spec.rb
104
+ - spec/spec_helper.rb
105
+ has_rdoc: true
106
+ homepage: http://github.com/robotarmy/acts_as_status_for
107
+ licenses:
108
+ - MIT
109
+ post_install_message:
110
+ rdoc_options: []
111
+
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ hash: -345335642477759870
120
+ segments:
121
+ - 0
122
+ version: "0"
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: "0"
129
+ requirements: []
130
+
131
+ rubyforge_project:
132
+ rubygems_version: 1.5.2
133
+ signing_key:
134
+ specification_version: 3
135
+ summary: Acts as Status For a list of events - did something occur? or not? with scoped finder?
136
+ test_files: []
137
+