diff --git a/CHANGELOG.md b/CHANGELOG.md index c877667..28ff647 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ # CHANGELOG -## 4.2.0 (not yet released) +## 4.3.0 (not yet released) + +## 4.2.0 + + * support turning off and on the configuration option for `no_direct_assignment` (see [issue #223](https://github.com/aasm/aasm/issues/223) for details) + * event arguments are now passed to `:after_commit` callbacks as well (see [issue #238](https://github.com/aasm/aasm/pull/238), thanks to [@kuinak](https://github.com/kuinak)) ## 4.1.1 diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..4394a22 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,13 @@ +# Contributor Code of Conduct + +As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. + +We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion. + +Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct. + +Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team. + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. + +This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/) diff --git a/README.md b/README.md index cf99ac5..06d2ec4 100644 --- a/README.md +++ b/README.md @@ -679,6 +679,15 @@ Feel free to * [Thorsten Böttger](http://github.com/alto) (since 2011) +## Contributing ## + +1. Read the [Contributor Code of Conduct](https://github.com/aasm/aasm/blob/master/CODE_OF_CONDUCT.md) +2. Fork it +3. Create your feature branch (git checkout -b my-new-feature) +4. Commit your changes (git commit -am 'Added some feature') +5. Push to the branch (git push origin my-new-feature) +6. Create new Pull Request + ## Warranty ## This software is provided "as is" and without any express or diff --git a/lib/aasm/base.rb b/lib/aasm/base.rb index 94e82d6..cdf78fb 100644 --- a/lib/aasm/base.rb +++ b/lib/aasm/base.rb @@ -29,11 +29,19 @@ module AASM configure :enum, nil - if @state_machine.config.no_direct_assignment - @klass.send(:define_method, "#{@state_machine.config.column}=") do |state_name| - raise AASM::NoDirectAssignmentError.new('direct assignment of AASM column has been disabled (see AASM configuration for this class)') + # make sure to raise an error if no_direct_assignment is enabled + # and attribute is directly assigned though + @klass.class_eval %Q( + def #{@state_machine.config.column}=(state_name) + if self.class.aasm.state_machine.config.no_direct_assignment + raise AASM::NoDirectAssignmentError.new( + 'direct assignment of AASM column has been disabled (see AASM configuration for this class)' + ) + else + super + end end - end + ) end # This method is both a getter and a setter diff --git a/lib/aasm/persistence/active_record_persistence.rb b/lib/aasm/persistence/active_record_persistence.rb index 1d404ea..4c80710 100644 --- a/lib/aasm/persistence/active_record_persistence.rb +++ b/lib/aasm/persistence/active_record_persistence.rb @@ -178,7 +178,7 @@ module AASM if success && options[:persist] event = self.class.aasm(state_machine_name).state_machine.events[name] - event.fire_callbacks(:after_commit, self) + event.fire_callbacks(:after_commit, self, *args) end success diff --git a/lib/aasm/version.rb b/lib/aasm/version.rb index 0a2f930..f637511 100644 --- a/lib/aasm/version.rb +++ b/lib/aasm/version.rb @@ -1,3 +1,3 @@ module AASM - VERSION = "4.1.1" + VERSION = "4.2.0" end diff --git a/spec/models/active_record/localizer_test_model.rb b/spec/models/active_record/localizer_test_model.rb index 1eb3c48..cb83402 100644 --- a/spec/models/active_record/localizer_test_model.rb +++ b/spec/models/active_record/localizer_test_model.rb @@ -1,8 +1,6 @@ class LocalizerTestModel < ActiveRecord::Base include AASM - attr_accessor :aasm_state - aasm do state :opened, :initial => true state :closed diff --git a/spec/models/validator.rb b/spec/models/validator.rb index 2da81ef..c23bd52 100644 --- a/spec/models/validator.rb +++ b/spec/models/validator.rb @@ -12,8 +12,8 @@ class Validator < ActiveRecord::Base transitions :to => :running, :from => :sleeping end event :sleep do - after_commit do - change_name_on_sleep + after_commit do |name| + change_name_on_sleep name end transitions :to => :sleeping, :from => :running end @@ -29,8 +29,8 @@ class Validator < ActiveRecord::Base save! end - def change_name_on_sleep - self.name = "sleeper" + def change_name_on_sleep name + self.name = name save! end diff --git a/spec/unit/persistence/active_record_persistence_spec.rb b/spec/unit/persistence/active_record_persistence_spec.rb index 29724a3..057e337 100644 --- a/spec/unit/persistence/active_record_persistence_spec.rb +++ b/spec/unit/persistence/active_record_persistence_spec.rb @@ -321,6 +321,24 @@ describe "direct assignment" do expect {obj.aasm_state = :running}.to raise_error(AASM::NoDirectAssignmentError) expect(obj.aasm_state.to_sym).to eql :pending end + + it 'can be turned off and on again' do + obj = NoDirectAssignment.create + expect(obj.aasm_state.to_sym).to eql :pending + + expect {obj.aasm_state = :running}.to raise_error(AASM::NoDirectAssignmentError) + expect(obj.aasm_state.to_sym).to eql :pending + + # allow it temporarily + NoDirectAssignment.aasm.state_machine.config.no_direct_assignment = false + obj.aasm_state = :pending + expect(obj.aasm_state.to_sym).to eql :pending + + # and forbid it again + NoDirectAssignment.aasm.state_machine.config.no_direct_assignment = true + expect {obj.aasm_state = :running}.to raise_error(AASM::NoDirectAssignmentError) + expect(obj.aasm_state.to_sym).to eql :pending + end end # direct assignment describe 'initial states' do @@ -436,7 +454,7 @@ describe 'transitions with persistence' do expect(validator).to be_running expect(validator.name).to eq("name changed") - validator.sleep! + validator.sleep!("sleeper") expect(validator).to be_sleeping expect(validator.name).to eq("sleeper") end