aasm 4.5.2 → 4.6.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 497994639dac4183fdf996cafad384f308784855
4
- data.tar.gz: 731e388e865ffb526e4cbb3c20443659aab8605a
3
+ metadata.gz: 5b8d8a7ce2210a49761c9f1c622393a1bc453452
4
+ data.tar.gz: 1239b8468c521eb11bdbfef04d58540a6d1e5b72
5
5
  SHA512:
6
- metadata.gz: b3572795abc66917c392e8736afc6d8ea882b12f0fdfcb15cd8046ae0f1977e8dd01b2eeab5d2fcb7e659295c3f45b6aa438aa008c61c8707370be7b27b40e31
7
- data.tar.gz: 42a4dc5b04c7a13e2a15e745a42b960a80cb4a90f4f62e1eb51c09b4493565314c85ae4aeeb1dd985fc85094706b440fc14ec11dbb7e7275c641e64309b7106d
6
+ metadata.gz: 7b8f2affa363cce45636f684c24e2f72d71cb0a5af4fce0303894a77eb9db7fb37f08442c4b4b1955d7aaae042225ec4b3e52b27ffafe5cbb0c765c15498cf65
7
+ data.tar.gz: d9769d7cfd98b28e98f8a79c9b0b823cabec8b721bf5bdfaaae3dac37ca2b6127c38b42d427ed2fd451a67987c2371e36f4b46df09e0b4042fb1abb2c44320cd
data/.gitignore CHANGED
@@ -17,3 +17,4 @@ alto
17
17
  .rspec
18
18
  .bundle
19
19
  tags
20
+ tmp
@@ -17,7 +17,7 @@ rvm:
17
17
  services: mongodb
18
18
 
19
19
  gemfile:
20
- - gemfiles/rails_3.2.gemfile
20
+ - gemfiles/rails_3.2_stable.gemfile
21
21
  - gemfiles/rails_4.0.gemfile
22
22
  - gemfiles/rails_4.0_mongo_mapper.gemfile
23
23
  - gemfiles/rails_4.1.gemfile
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 4.6.0
4
+
5
+ * fix: make sure the column is actually present for _ActiveRecord_ enums (see [issue #265](https://github.com/aasm/aasm/issues/265) and [issue #152](https://github.com/aasm/aasm/issues/152) for details, thanks to [@anilmaurya](https://github.com/anilmaurya))
6
+ * add generators to configure active_record and mongoid after install (see [issue #261](https://github.com/aasm/aasm/issues/261) for details, thanks to [@anilmaurya](https://github.com/anilmaurya))
7
+
3
8
  ## 4.5.2
4
9
 
5
10
  * fix arity difference between Procs and lambdas (see [issue #293](https://github.com/aasm/aasm/issues/293) for details)
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2006-2015 Scott Barron
1
+ Copyright (c) 2006-2016 Scott Barron
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -791,6 +791,17 @@ gem 'aasm'
791
791
  % sudo gem install pkg/aasm-x.y.z.gem
792
792
  ```
793
793
 
794
+ ### Generators
795
+
796
+ After installing Aasm you can run generator:
797
+
798
+ ```sh
799
+ % rails generate aasm NAME [COLUMN_NAME]
800
+ ```
801
+ Replace NAME with the Model name, COLUMN_NAME is optional(default is 'aasm_state').
802
+ This will create a model (if one does not exist) and configure it with aasm block.
803
+ For Active record orm a migration file is added to add aasm state column to table.
804
+
794
805
  ## Latest changes ##
795
806
 
796
807
  Take a look at the [CHANGELOG](https://github.com/aasm/aasm/blob/master/CHANGELOG.md) for details about recent changes to the current version.
@@ -828,7 +839,7 @@ purpose.
828
839
 
829
840
  ## License ##
830
841
 
831
- Copyright (c) 2006-2015 Scott Barron
842
+ Copyright (c) 2006-2016 Scott Barron
832
843
 
833
844
  Permission is hereby granted, free of charge, to any person obtaining
834
845
  a copy of this software and associated documentation files (the
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.add_development_dependency 'rake'
20
20
  s.add_development_dependency 'sdoc'
21
21
  s.add_development_dependency 'rspec', ">= 3"
22
+ s.add_development_dependency 'generator_spec'
22
23
 
23
24
  # debugging
24
25
  # s.add_development_dependency 'debugger'
@@ -5,10 +5,11 @@ gem 'rubysl', :platforms => :rbx
5
5
  gem 'rubinius-developer_tools', :platforms => :rbx
6
6
  gem "jruby-openssl", :platforms => :jruby
7
7
  gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
8
- gem "rails", "3.2.21"
8
+ gem "rails", :github => "rails/rails", :branch => "3-2-stable"
9
9
  gem 'mongoid', '~>3.1' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
10
10
  gem 'sequel'
11
11
  gem 'mongo_mapper', '~>0.13'
12
12
  gem 'bson_ext', :platforms => :ruby
13
+ gem 'test-unit', '~> 3.0'
13
14
 
14
15
  gemspec :path => "../"
@@ -102,7 +102,10 @@ module AASM
102
102
  end
103
103
 
104
104
  def aasm_column_looks_like_enum(name=:default)
105
- self.class.columns_hash[self.class.aasm(name).attribute_name.to_s].type == :integer
105
+ column_name = self.class.aasm(name).attribute_name.to_s
106
+ column = self.class.columns_hash[column_name]
107
+ raise NoMethodError.new("undefined method '#{column_name}' for #{self.class}") if column.nil?
108
+ column.type == :integer
106
109
  end
107
110
 
108
111
  def aasm_guess_enum_method(name=:default)
@@ -1,3 +1,3 @@
1
1
  module AASM
2
- VERSION = "4.5.2"
2
+ VERSION = "4.6.0"
3
3
  end
@@ -0,0 +1,17 @@
1
+ require 'rails/generators/named_base'
2
+
3
+ module AASM
4
+ module Generators
5
+ class AASMGenerator < Rails::Generators::NamedBase
6
+
7
+ source_root File.expand_path("../templates", __FILE__)
8
+ argument :column_name, type: :string, default: 'aasm_state'
9
+
10
+ desc "Generates a model with the given NAME (if one does not exist) with aasm " <<
11
+ "block and migration to add aasm_state column."
12
+
13
+ hook_for :orm
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,35 @@
1
+ module AASM
2
+ module Generators
3
+ module OrmHelpers
4
+
5
+ def model_contents
6
+ if column_name == 'aasm_state'
7
+ <<RUBY
8
+ include AASM
9
+
10
+ aasm do
11
+ end
12
+ RUBY
13
+ else
14
+ <<RUBY
15
+ include AASM
16
+
17
+ aasm :column => '#{column_name}' do
18
+ end
19
+ RUBY
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def model_exists?
26
+ File.exists?(File.join(destination_root, model_path))
27
+ end
28
+
29
+ def model_path
30
+ @model_path ||= File.join("app", "models", "#{file_path}.rb")
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,38 @@
1
+ require 'rails/generators/active_record'
2
+ require 'generators/aasm/orm_helpers'
3
+
4
+ module ActiveRecord
5
+ module Generators
6
+ class AASMGenerator < ActiveRecord::Generators::Base
7
+ include AASM::Generators::OrmHelpers
8
+
9
+ argument :column_name, type: :string, default: 'aasm_state'
10
+
11
+ source_root File.expand_path("../templates", __FILE__)
12
+
13
+ def copy_aasm_migration
14
+ if model_exists?
15
+ migration_template "migration_existing.rb", "db/migrate/add_aasm_state_to_#{table_name}.rb"
16
+ else
17
+ migration_template "migration.rb", "db/migrate/aasm_create_#{table_name}.rb"
18
+ end
19
+ end
20
+
21
+ def generate_model
22
+ invoke "active_record:model", [name], migration: false unless model_exists?
23
+ end
24
+
25
+ def inject_aasm_content
26
+ content = model_contents
27
+
28
+ class_path = if namespaced?
29
+ class_name.to_s.split("::")
30
+ else
31
+ [class_name]
32
+ end
33
+ inject_into_class(model_path, class_path.last, content) if model_exists?
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,8 @@
1
+ class AASMCreate<%= table_name.camelize %> < ActiveRecord::Migration
2
+ def change
3
+ create_table(:<%= table_name %>) do |t|
4
+ t.string :<%= column_name %>
5
+ t.timestamps null: false
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+ class AddAASMTo<%= table_name.camelize %> < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :<%= table_name %>, :<%= column_name %>, :string
4
+ end
5
+
6
+ def self.down
7
+ remove_column :<%= table_name %>, :<%= column_name %>
8
+ end
9
+ end
@@ -0,0 +1,28 @@
1
+ require 'rails/generators/named_base'
2
+ require 'generators/aasm/orm_helpers'
3
+
4
+ module Mongoid
5
+ module Generators
6
+ class AASMGenerator < Rails::Generators::NamedBase
7
+ include AASM::Generators::OrmHelpers
8
+
9
+ argument :column_name, type: :string, default: 'aasm_state'
10
+
11
+ def generate_model
12
+ invoke "mongoid:model", [name] unless model_exists?
13
+ end
14
+
15
+ def inject_aasm_content
16
+ inject_into_file model_path, model_contents, after: "include Mongoid::Document\n" if model_exists?
17
+ end
18
+
19
+ def inject_field_types
20
+ inject_into_file model_path, migration_data, after: "include Mongoid::Document\n" if model_exists?
21
+ end
22
+
23
+ def migration_data
24
+ " field :#{column_name}"
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,5 +1,5 @@
1
1
  ActiveRecord::Migration.suppress_messages do
2
- %w{gates multiple_gates readers writers transients simples no_scopes multiple_no_scopes no_direct_assignments multiple_no_direct_assignments thieves multiple_thieves localizer_test_models persisted_states provided_and_persisted_states with_enums with_true_enums with_false_enums false_states multiple_with_enums multiple_with_true_enums multiple_with_false_enums multiple_false_states readme_jobs}.each do |table_name|
2
+ %w{gates multiple_gates readers writers transients simples no_scopes multiple_no_scopes no_direct_assignments multiple_no_direct_assignments thieves multiple_thieves localizer_test_models persisted_states provided_and_persisted_states with_enums with_enum_without_columns multiple_with_enum_without_columns with_true_enums with_false_enums false_states multiple_with_enums multiple_with_true_enums multiple_with_false_enums multiple_false_states readme_jobs}.each do |table_name|
3
3
  ActiveRecord::Migration.create_table table_name, :force => true do |t|
4
4
  t.string "aasm_state"
5
5
  end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+ require 'generator_spec'
3
+ require 'generators/active_record/aasm_generator'
4
+
5
+ describe ActiveRecord::Generators::AASMGenerator, type: :generator do
6
+ destination File.expand_path("../../../tmp", __FILE__)
7
+
8
+ before(:all) do
9
+ prepare_destination
10
+ end
11
+
12
+ it "creates model with aasm block for default column_name" do
13
+ run_generator %w(user)
14
+ assert_file "app/models/user.rb", /include AASM\n\n aasm do\n end\n/
15
+ end
16
+
17
+ it "creates model with aasm block for custom column_name" do
18
+ run_generator %w(user state)
19
+ assert_file "app/models/user.rb", /aasm :column => 'state' do\n end\n/
20
+ end
21
+
22
+ it "creates model with aasm block for namespaced model" do
23
+ run_generator %w(Admin::User state)
24
+ assert_file "app/models/admin/user.rb", /aasm :column => 'state' do\n end\n/
25
+ end
26
+
27
+ it "creates migration for model with aasm_column" do
28
+ run_generator %w(post)
29
+ assert_migration "db/migrate/aasm_create_posts.rb", /create_table(:posts) do |t|\n t.string :aasm_state\n/
30
+ end
31
+
32
+ it "add aasm_column in existing model" do
33
+ run_generator %w(job)
34
+ assert_file "app/models/job.rb"
35
+ run_generator %w(job)
36
+ assert_migration "db/migrate/add_aasm_state_to_jobs.rb"
37
+ end
38
+
39
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+ require 'generator_spec'
3
+ require 'generators/mongoid/aasm_generator'
4
+
5
+ begin
6
+ require "mongoid"
7
+
8
+ describe Mongoid::Generators::AASMGenerator, type: :generator do
9
+ destination File.expand_path("../../../tmp", __FILE__)
10
+
11
+ before(:all) do
12
+ prepare_destination
13
+ end
14
+
15
+ it "creates model with aasm block for default column_name" do
16
+ run_generator %w(user)
17
+ assert_file "app/models/user.rb", /include AASM\n\n aasm do\n end\n/
18
+ end
19
+
20
+ it "creates model with aasm block for custom column_name" do
21
+ run_generator %w(user state)
22
+ assert_file "app/models/user.rb", /aasm :column => 'state' do\n end\n/
23
+ end
24
+
25
+ it "creates model with aasm block for namespaced model" do
26
+ run_generator %w(Admin::User state)
27
+ assert_file "app/models/admin/user.rb", /aasm :column => 'state' do\n end\n/
28
+ end
29
+
30
+ end
31
+ rescue LoadError
32
+ puts "Not running Mongoid specs because mongoid gem is not installed!!!"
33
+ end
@@ -0,0 +1,38 @@
1
+ class WithEnumWithoutColumn < ActiveRecord::Base
2
+ include AASM
3
+
4
+ if ActiveRecord::VERSION::MAJOR >= 4 && ActiveRecord::VERSION::MINOR >= 1 # won't work with Rails <= 4.1
5
+ enum status: {
6
+ opened: 0,
7
+ closed: 1
8
+ }
9
+ end
10
+
11
+ aasm :column => :status do
12
+ state :closed, initial: true
13
+ state :opened
14
+
15
+ event :view do
16
+ transitions :to => :opened, :from => :closed
17
+ end
18
+ end
19
+ end
20
+
21
+ class MultipleWithEnumWithoutColumn < ActiveRecord::Base
22
+ include AASM
23
+ if ActiveRecord::VERSION::MAJOR >= 4 && ActiveRecord::VERSION::MINOR >= 1 # won't work with Rails <= 4.1
24
+ enum status: {
25
+ opened: 0,
26
+ closed: 1
27
+ }
28
+ end
29
+
30
+ aasm :left, :column => :status do
31
+ state :closed, initial: true
32
+ state :opened
33
+
34
+ event :view do
35
+ transitions :to => :opened, :from => :closed
36
+ end
37
+ end
38
+ end
@@ -113,6 +113,20 @@ describe "instance methods" do
113
113
  end
114
114
  end
115
115
  end
116
+
117
+ if ActiveRecord::VERSION::MAJOR >= 4 && ActiveRecord::VERSION::MINOR >= 1 # won't work with Rails <= 4.1
118
+ # Enum are introduced from Rails 4.1, therefore enum syntax will not work on Rails <= 4.1
119
+ context "when AASM enum setting is not enabled and aasm column not present" do
120
+
121
+ let(:multiple_with_enum_without_column) {MultipleWithEnumWithoutColumn.new}
122
+
123
+ it "should raise NoMethodError for transitions" do
124
+ expect{multiple_with_enum_without_column.send(:view, :left)}.to raise_error(NoMethodError, "undefined method 'status' for MultipleWithEnumWithoutColumn")
125
+ end
126
+ end
127
+
128
+ end
129
+
116
130
  end
117
131
 
118
132
  context "when AASM is configured to use enum" do
@@ -113,6 +113,20 @@ describe "instance methods" do
113
113
  end
114
114
  end
115
115
  end
116
+
117
+ if ActiveRecord::VERSION::MAJOR >= 4 && ActiveRecord::VERSION::MINOR >= 1 # won't work with Rails <= 4.1
118
+ # Enum are introduced from Rails 4.1, therefore enum syntax will not work on Rails <= 4.1
119
+ context "when AASM enum setting is not enabled and aasm column not present" do
120
+
121
+ let(:with_enum_without_column) {WithEnumWithoutColumn.new}
122
+
123
+ it "should raise NoMethodError for transitions" do
124
+ expect{with_enum_without_column.send(:view)}.to raise_error(NoMethodError, "undefined method 'status' for WithEnumWithoutColumn")
125
+ end
126
+ end
127
+
128
+ end
129
+
116
130
  end
117
131
 
118
132
  context "when AASM is configured to use enum" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aasm
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.5.2
4
+ version: 4.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Barron
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-01-06 00:00:00.000000000 Z
13
+ date: 2016-01-08 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -54,6 +54,20 @@ dependencies:
54
54
  - - ">="
55
55
  - !ruby/object:Gem::Version
56
56
  version: '3'
57
+ - !ruby/object:Gem::Dependency
58
+ name: generator_spec
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
57
71
  - !ruby/object:Gem::Dependency
58
72
  name: pry
59
73
  requirement: !ruby/object:Gem::Requirement
@@ -90,7 +104,7 @@ files:
90
104
  - Rakefile
91
105
  - aasm.gemspec
92
106
  - callbacks.txt
93
- - gemfiles/rails_3.2.gemfile
107
+ - gemfiles/rails_3.2_stable.gemfile
94
108
  - gemfiles/rails_4.0.gemfile
95
109
  - gemfiles/rails_4.0_mongo_mapper.gemfile
96
110
  - gemfiles/rails_4.1.gemfile
@@ -123,10 +137,18 @@ files:
123
137
  - lib/aasm/rspec/transition_from.rb
124
138
  - lib/aasm/state_machine.rb
125
139
  - lib/aasm/version.rb
140
+ - lib/generators/aasm/aasm_generator.rb
141
+ - lib/generators/aasm/orm_helpers.rb
142
+ - lib/generators/active_record/aasm_generator.rb
143
+ - lib/generators/active_record/templates/migration.rb
144
+ - lib/generators/active_record/templates/migration_existing.rb
145
+ - lib/generators/mongoid/aasm_generator.rb
126
146
  - spec/database.rb
127
147
  - spec/database.yml
128
148
  - spec/en.yml
129
149
  - spec/en_deprecated_style.yml
150
+ - spec/generators/active_record_generator_spec.rb
151
+ - spec/generators/mongoid_generator_spec.rb
130
152
  - spec/models/active_record/basic_active_record_two_state_machines_example.rb
131
153
  - spec/models/active_record/complex_active_record_example.rb
132
154
  - spec/models/active_record/derivate_new_dsl.rb
@@ -143,6 +165,7 @@ files:
143
165
  - spec/models/active_record/thief.rb
144
166
  - spec/models/active_record/transient.rb
145
167
  - spec/models/active_record/with_enum.rb
168
+ - spec/models/active_record/with_enum_without_column.rb
146
169
  - spec/models/active_record/with_false_enum.rb
147
170
  - spec/models/active_record/with_true_enum.rb
148
171
  - spec/models/active_record/writer.rb
@@ -265,6 +288,8 @@ test_files:
265
288
  - spec/database.yml
266
289
  - spec/en.yml
267
290
  - spec/en_deprecated_style.yml
291
+ - spec/generators/active_record_generator_spec.rb
292
+ - spec/generators/mongoid_generator_spec.rb
268
293
  - spec/models/active_record/basic_active_record_two_state_machines_example.rb
269
294
  - spec/models/active_record/complex_active_record_example.rb
270
295
  - spec/models/active_record/derivate_new_dsl.rb
@@ -281,6 +306,7 @@ test_files:
281
306
  - spec/models/active_record/thief.rb
282
307
  - spec/models/active_record/transient.rb
283
308
  - spec/models/active_record/with_enum.rb
309
+ - spec/models/active_record/with_enum_without_column.rb
284
310
  - spec/models/active_record/with_false_enum.rb
285
311
  - spec/models/active_record/with_true_enum.rb
286
312
  - spec/models/active_record/writer.rb