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

Add create_activity! method. #334

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions lib/public_activity/common.rb
Expand Up @@ -259,6 +259,20 @@ def create_activity(*args)
nil
end

# Directly saves activity to database. Works the same as create_activity
# but throws validation error for each supported ORM.
#
# @see #create_activity
def create_activity!(*args)
return unless self.public_activity_enabled?
options = prepare_settings(*args)

if call_hook_safe(options[:key].split('.').last)
reset_activity_instance_options
return PublicActivity::Adapter.create_activity!(self, options)
end
end

# Prepares settings used during creation of Activity record.
# params passed directly to tracked model have priority over
# settings specified in tracked() method
Expand Down
5 changes: 5 additions & 0 deletions lib/public_activity/orm/active_record/adapter.rb
Expand Up @@ -12,6 +12,11 @@ class Adapter
def self.create_activity(trackable, options)
trackable.activities.create options
end

# Creates activity on `trackable` with `options`; throws error on validation failure
def self.create_activity!(trackable, options)
trackable.activities.create! options
end
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/public_activity/orm/mongo_mapper/adapter.rb
Expand Up @@ -8,6 +8,11 @@ class Adapter
def self.create_activity(trackable, options)
trackable.activities.create options
end

# Creates activity on `trackable` with `options`; throws error on validation failure
def self.create_activity!(trackable, options)
trackable.activities.create! options
end
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/public_activity/orm/mongoid/adapter.rb
Expand Up @@ -8,6 +8,11 @@ class Adapter
def self.create_activity(trackable, options)
trackable.activities.create options
end

# Creates activity on `trackable` with `options`; throws error on validation failure
def self.create_activity!(trackable, options)
trackable.activities.create! options
end
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions test/test_common.rb
Expand Up @@ -50,6 +50,13 @@
subject.create_activity("some.key").wont_be_nil
end

it '#create_activity! returns a new activity object' do
subject.save
activity = subject.create_activity!("some.key")
assert activity.persisted?
assert_equal 'article.some.key', activity.key
end

it 'update action should not create activity on save unless model changed' do
subject.save
before_count = subject.activities.count
Expand Down