acts_as_discontinued 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.DS_Store ADDED
Binary file
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in acts_as_discontinued.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Emmanuel Nicolau
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,29 @@
1
+ # ActsAsDiscontinued
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'acts_as_discontinued'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install acts_as_discontinued
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'lib/acts_as_discontinued'
5
+ t.test_files = FileList['test/acts_as_discontinued/*_test.rb']
6
+ t.verbose = true
7
+ end
8
+ task :default => :test
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'acts_as_discontinued/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "acts_as_discontinued"
8
+ gem.version = ActsAsDiscontinued::VERSION
9
+ gem.authors = ["Emmanuel Nicolau"]
10
+ gem.email = ["emmanicolau@gmail.com"]
11
+ gem.description = %q{Another paranoid gem}
12
+ gem.summary = %q{Another paranoid gem}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ gem.add_dependency "activerecord", '>= 3.0.0'
20
+ gem.add_development_dependency "sqlite3"
21
+ end
@@ -0,0 +1,59 @@
1
+ module ActsAsDiscontinued
2
+ def self.included(base)
3
+ base.extend ClassMethods
4
+ end
5
+
6
+ module ClassMethods
7
+ def acts_as_discontinued(options = {})
8
+ include InstanceMethods
9
+ class_eval do
10
+ scope :active, :conditions => {:discontinued_at => nil}
11
+ scope :status, lambda { |*args|
12
+ stat = args.first.to_s
13
+ case stat
14
+ when 'active', ''
15
+ {:conditions => {:discontinued_at => nil}}
16
+ when 'inactive'
17
+ {:conditions => "#{table_name}.discontinued_at is not null"}
18
+ else
19
+ {}
20
+ end
21
+ }
22
+ end
23
+ end
24
+ end
25
+
26
+ module InstanceMethods
27
+ def discontinue!
28
+ self.update_attribute :discontinued_at, Time.now
29
+ self
30
+ end
31
+
32
+ def activate!
33
+ self.update_attribute :discontinued_at, nil
34
+ self
35
+ end
36
+
37
+ def discontinued? date = Time.now
38
+ not discontinued_at.nil? and discontinued_at <= date
39
+ end
40
+ alias inactive? discontinued?
41
+
42
+ def active? date = Time.now
43
+ not discontinued? date
44
+ end
45
+ alias active active?
46
+
47
+ def active= active
48
+ if ['true', '1'].include?(active.to_s)
49
+ self.discontinued_at = nil
50
+ else
51
+ self.discontinued_at ||= Time.now
52
+ end
53
+ end
54
+
55
+ def activation_status
56
+ active? ? 'Activo' : 'Inactivo'
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsDiscontinued
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,3 @@
1
+ require "acts_as_discontinued/version"
2
+ require "acts_as_discontinued/acts_as_discontinued"
3
+ ActiveRecord::Base.class_eval { include ActsAsDiscontinued }
data/test/.DS_Store ADDED
Binary file
@@ -0,0 +1,106 @@
1
+ require 'test/unit'
2
+ require 'active_record'
3
+ require "#{File.dirname(__FILE__)}/../../lib/acts_as_discontinued"
4
+
5
+ ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
6
+
7
+ def setup_db
8
+ ActiveRecord::Schema.define(version: 1) do
9
+ create_table :products do |t|
10
+ t.column :discontinued_at, :datetime
11
+ t.references :category
12
+ end
13
+ create_table :categories do |t|
14
+ t.column :discontinued_at, :datetime
15
+ end
16
+ end
17
+ end
18
+
19
+ def teardown_db
20
+ ActiveRecord::Base.connection.tables.each do |table|
21
+ ActiveRecord::Base.connection.drop_table(table)
22
+ end
23
+ end
24
+
25
+ class Category < ActiveRecord::Base
26
+ acts_as_discontinued
27
+ has_many :products
28
+ end
29
+
30
+ class Product < ActiveRecord::Base
31
+ acts_as_discontinued
32
+ belongs_to :category
33
+ end
34
+
35
+ class ActsAsDiscontinuedTest < Test::Unit::TestCase
36
+ def test_should_set_discontinued_at_when_discontinued!
37
+ assert_not_nil Product.create!.discontinue!.reload.discontinued_at
38
+ end
39
+
40
+ def test_should_unset_discontinued_at_when_activate!
41
+ p = Product.create! discontinued_at: Time.now
42
+ assert_not_nil p.discontinued_at
43
+ p.activate!
44
+ assert_nil p.reload.discontinued_at
45
+ end
46
+
47
+ def test_should_provide_active_scope
48
+ 2.times { Product.create! }
49
+ Product.create!.discontinue!
50
+ assert_equal 2, Product.active.size
51
+ assert_equal 3, Product.count
52
+ end
53
+
54
+ def test_should_provide_status_scope
55
+ 2.times { Product.create! }
56
+ Product.create!(category: Category.create!).discontinue!
57
+ assert_equal 3, Product.status(:all).size
58
+ assert_equal 2, Product.status(:active).size
59
+ assert_equal 2, Product.status(nil).size
60
+ assert_equal 1, Product.status(:inactive).size
61
+ assert_equal 1, Product.status(:inactive).find(:all, joins: :category).size
62
+ end
63
+
64
+ def test_scope_should_work_on_strings
65
+ 2.times { Product.create! }
66
+ Product.create!(category: Category.create!).discontinue!
67
+ assert_equal 3, Product.status('all').size
68
+ assert_equal 2, Product.status('active').size
69
+ assert_equal 2, Product.status('').size
70
+ assert_equal 1, Product.status('inactive').size
71
+ end
72
+
73
+ def test_should_provide_discountinued?
74
+ assert !Product.create!.discontinued?
75
+ assert Product.create!.discontinue!.discontinued?
76
+ end
77
+
78
+ def test_should_provide_active?
79
+ assert Product.create!.active?
80
+ assert !Product.create!.discontinue!.active?
81
+ end
82
+
83
+ def test_should_provide_active_setter
84
+ p = Product.new
85
+ p.active = false
86
+ assert !p.active?
87
+ p.active = true
88
+ assert p.active?
89
+ end
90
+
91
+ def test_setter_should_not_update_discontinued_at_if_was_already_discontinued
92
+ p = Product.new active: false
93
+ original_discontinued_at = p.discontinued_at
94
+ assert original_discontinued_at
95
+ p.active = false
96
+ assert_equal original_discontinued_at, p.discontinued_at
97
+ end
98
+
99
+ def setup
100
+ setup_db
101
+ end
102
+
103
+ def teardown
104
+ teardown_db
105
+ end
106
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_discontinued
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Emmanuel Nicolau
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-14 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
+ - !ruby/object:Gem::Dependency
31
+ name: sqlite3
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
+ description: Another paranoid gem
47
+ email:
48
+ - emmanicolau@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .DS_Store
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - acts_as_discontinued.gemspec
60
+ - lib/acts_as_discontinued.rb
61
+ - lib/acts_as_discontinued/acts_as_discontinued.rb
62
+ - lib/acts_as_discontinued/version.rb
63
+ - test/.DS_Store
64
+ - test/acts_as_discontinued/acts_as_discontinued_test.rb
65
+ homepage: ''
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ segments:
78
+ - 0
79
+ hash: 2844659536037982857
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ segments:
87
+ - 0
88
+ hash: 2844659536037982857
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 1.8.23
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Another paranoid gem
95
+ test_files:
96
+ - test/.DS_Store
97
+ - test/acts_as_discontinued/acts_as_discontinued_test.rb