Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepend timestamps with namespace to avoid naming conflicts #754

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/aasm/base.rb
Expand Up @@ -55,7 +55,7 @@ def initialize(klass, name, state_machine, options={}, &block)
configure :logger, Logger.new(STDERR)

# setup timestamp-setting callback if enabled
setup_timestamps(@name)
setup_timestamps(@name, namespace)

# make sure to raise an error if no_direct_assignment is enabled
# and attribute is directly assigned though
Expand Down Expand Up @@ -263,12 +263,13 @@ def skip_instance_level_validation(event, name, aasm_name, klass)
end
end

def setup_timestamps(aasm_name)
def setup_timestamps(aasm_name, namespace)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method setup_timestamps has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.

return unless @state_machine.config.timestamps

after_all_transitions do
if self.class.aasm(:"#{aasm_name}").state_machine.config.timestamps
ts_setter = "#{aasm(aasm_name).to_state}_at="
base_setter = "#{aasm(aasm_name).to_state}_at="
ts_setter = !!namespace ? "#{namespace}_#{base_setter}" : base_setter
respond_to?(ts_setter) && send(ts_setter, ::Time.now)
end
end
Expand Down
13 changes: 13 additions & 0 deletions spec/models/timestamps_with_namespace_machine_example.rb
@@ -0,0 +1,13 @@
class TimestampsWithNamespaceMachineExample
include AASM

attr_accessor :new_opened_at

aasm :my_state, timestamps: true, namespace: :new do
state :opened

event :open do
transitions to: :opened
end
end
end
5 changes: 5 additions & 0 deletions spec/unit/timestamps_spec.rb
Expand Up @@ -29,4 +29,9 @@
object = TimestampsWithNamedMachineExample.new
expect { object.open }.to change { object.opened_at }.from(nil).to(instance_of(::Time))
end

it 'calls a timestamp setter based on the state name and namespace when entering a new state using a namespace state machine' do
object = TimestampsWithNamespaceMachineExample.new
expect { object.open }.to change { object.new_opened_at }.from(nil).to(instance_of(::Time))
end
end