acts_as_model_with_status 0.9.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,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in acts_as_model_with_status.gemspec
4
+ gemspec
5
+
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ desc "Run tests"
7
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "acts_as_model_with_status/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "acts_as_model_with_status"
7
+ s.version = ActsAsModelWithStatus::VERSION
8
+ s.authors = ["Santi Bel"]
9
+ s.email = ["santiago.bel@gmail.com"]
10
+ s.homepage = "http://github.com/landtax/acts_as_model_with_status"
11
+ s.summary = %q{Simple status management for Active Record models}
12
+ s.description = %q{Simple status management for Active Record models}
13
+
14
+ s.rubyforge_project = "acts_as_model_with_status"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rspec", "~> 2.13"
22
+ s.add_development_dependency "sqlite3", "1.3.7"
23
+
24
+ s.add_runtime_dependency "rails", "~> 3.2"
25
+ end
@@ -0,0 +1,10 @@
1
+ require "acts_as_model_with_status/version"
2
+ require 'active_support/core_ext'
3
+
4
+ require File.join(File.dirname(__FILE__), "acts_as_model_with_status/railtie.rb")
5
+
6
+ module ActsAsModelWithStatus
7
+ autoload :Hook, File.join(File.dirname(__FILE__), "acts_as_model_with_status/hook")
8
+ autoload :InstanceMethods, File.join(File.dirname(__FILE__), "acts_as_model_with_status/instance_methods")
9
+ end
10
+
@@ -0,0 +1,21 @@
1
+ module ActsAsModelWithStatus::Hook
2
+
3
+ def acts_as_model_with_status(status_list, *args)
4
+ options = args.extract_options!
5
+
6
+ cattr_accessor :available_statuses
7
+ cattr_accessor :invert_available_statuses
8
+ cattr_accessor :status_column
9
+ cattr_accessor :default_status
10
+
11
+ self.available_statuses = status_list
12
+ self.invert_available_statuses = self.available_statuses.invert
13
+ self.status_column = options[:column] || :status
14
+ self.default_status = options[:default]
15
+
16
+ include ActsAsModelWithStatus::InstanceMethods
17
+
18
+ after_initialize :set_default_status_if_needed
19
+ end
20
+
21
+ end
@@ -0,0 +1,37 @@
1
+ module ActsAsModelWithStatus::InstanceMethods
2
+
3
+ def status
4
+ status_key read_attribute(self.status_column.to_sym)
5
+ end
6
+
7
+ def status= key
8
+ validate_status key
9
+
10
+ write_attribute(self.status_column.to_sym, status_value(key))
11
+ end
12
+
13
+ private
14
+
15
+ def set_default_status_if_needed
16
+ if self.status.nil? && !self.default_status.nil?
17
+ self.status = self.default_status
18
+ end
19
+ end
20
+
21
+ def status_key(value)
22
+ invert_available_statuses[value]
23
+ end
24
+
25
+ def status_value(key)
26
+ validate_status key
27
+
28
+ available_statuses[key]
29
+ end
30
+
31
+ def validate_status(key)
32
+ if !available_statuses.keys.include? key
33
+ raise "Invalid status ':#{key}'"
34
+ end
35
+ end
36
+
37
+ end
@@ -0,0 +1,16 @@
1
+ require 'rails'
2
+ require 'active_record'
3
+ require 'acts_as_model_with_status'
4
+
5
+ begin
6
+ module ActsAsModelWithStatus
7
+ class Railtie < Rails::Railtie
8
+ config.to_prepare do
9
+ ActiveRecord::Base.send(:extend, ActsAsModelWithStatus::Hook)
10
+ end
11
+ end
12
+ end
13
+ rescue
14
+ p $!, $!.message
15
+ raise $!
16
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsModelWithStatus
2
+ VERSION = "0.9.0"
3
+ end
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+
3
+ ActiveRecord::Base.send(:extend, ActsAsModelWithStatus::Hook)
4
+
5
+ STATUS_LIST = {:new => 1, :running => 2, :done => 3, :closed => 4}
6
+
7
+ class MyModel < ActiveRecord::Base
8
+ end
9
+
10
+ class BasicModel < MyModel
11
+ acts_as_model_with_status(STATUS_LIST)
12
+ end
13
+
14
+ class WithDefaultModel < MyModel
15
+ acts_as_model_with_status(STATUS_LIST, :default => :running)
16
+ end
17
+
18
+ class WithColumnModel < MyModel
19
+ acts_as_model_with_status(STATUS_LIST, :default => :running, :column => :my_status)
20
+ end
21
+
22
+
23
+ describe ActsAsModelWithStatus do
24
+
25
+ before :each do
26
+ initialize_database
27
+ end
28
+
29
+ describe "Status operations" do
30
+ subject { BasicModel.new }
31
+
32
+ its(:status_culumn) { eq :status}
33
+ its(:default_status) {be_nil}
34
+ its (:status) { be_nil }
35
+
36
+ it "must update status" do
37
+ STATUS_LIST.each do |k,v|
38
+ subject.status = k
39
+ subject.save
40
+
41
+ subject.reload
42
+ subject.status.should == k
43
+ end
44
+ end
45
+
46
+ it "must raise an exception when status is not in list" do
47
+ expect{subject.status = :fake_status}.to raise_exception
48
+ end
49
+
50
+ end
51
+
52
+ describe "Initialize Status with default value" do
53
+ subject { WithDefaultModel.new }
54
+
55
+ its(:status_culumn) { eq :status}
56
+ its(:default_status) {eq :running}
57
+ its(:status) {eq :running }
58
+ end
59
+
60
+ describe "Use custom column name" do
61
+ subject { WithColumnModel.new }
62
+
63
+ its(:status_culumn) {eq :my_status}
64
+ its(:default_status) {eq :running}
65
+
66
+ it "must initialize status" do
67
+ subject.status.should == :running
68
+ end
69
+
70
+ it "does not store value in default column name but in custom column name" do
71
+ subject.save
72
+ subject.reload
73
+ subject.read_attribute(:status).should be_nil
74
+ subject.read_attribute(:my_status).should == 2
75
+
76
+ subject.status = :new
77
+ subject.save
78
+ subject.reload
79
+ subject.read_attribute(:status).should be_nil
80
+ subject.read_attribute(:my_status).should == 1
81
+ end
82
+
83
+ end
84
+
85
+
86
+ end
@@ -0,0 +1,30 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'rubygems'
5
+ require 'bundler'
6
+ Bundler.setup(:default, :development)
7
+
8
+ require 'active_record'
9
+ require 'active_support'
10
+ require 'acts_as_model_with_status'
11
+ require 'rspec'
12
+ require 'rspec/autorun'
13
+
14
+
15
+ RSpec.configure do |config|
16
+ config.color_enabled = true
17
+ config.formatter = 'documentation'
18
+ end
19
+
20
+ def initialize_database
21
+ ActiveRecord::Base.establish_connection( :adapter => "sqlite3", :database => ":memory:")
22
+ ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS 'my_models'")
23
+ ActiveRecord::Base.connection.create_table(:my_models) do |t|
24
+ t.integer :my_status
25
+ t.integer :status
26
+ t.timestamps
27
+ end
28
+ end
29
+
30
+ initialize_database
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_model_with_status
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Santi Bel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-08 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &80799740 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.13'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *80799740
25
+ - !ruby/object:Gem::Dependency
26
+ name: sqlite3
27
+ requirement: &80799330 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - =
31
+ - !ruby/object:Gem::Version
32
+ version: 1.3.7
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *80799330
36
+ - !ruby/object:Gem::Dependency
37
+ name: rails
38
+ requirement: &80799020 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '3.2'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *80799020
47
+ description: Simple status management for Active Record models
48
+ email:
49
+ - santiago.bel@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - Rakefile
57
+ - acts_as_model_with_status.gemspec
58
+ - lib/acts_as_model_with_status.rb
59
+ - lib/acts_as_model_with_status/hook.rb
60
+ - lib/acts_as_model_with_status/instance_methods.rb
61
+ - lib/acts_as_model_with_status/railtie.rb
62
+ - lib/acts_as_model_with_status/version.rb
63
+ - spec/acts_as_model_with_status_spec.rb
64
+ - spec/spec_helper.rb
65
+ homepage: http://github.com/landtax/acts_as_model_with_status
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
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project: acts_as_model_with_status
85
+ rubygems_version: 1.8.10
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Simple status management for Active Record models
89
+ test_files: []