Skip to content

Commit

Permalink
Refactor and fix linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ur5us committed Jan 19, 2024
1 parent 33d830f commit ae1c60a
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 51 deletions.
7 changes: 4 additions & 3 deletions lib/public_activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,21 @@ def self.config

# Method used to choose which ORM to load
# when PublicActivity::Activity class is being autoloaded
def self.inherit_orm(model="Activity")
def self.inherit_orm(model = 'Activity')
orm = PublicActivity.config.orm
require "public_activity/orm/#{orm.to_s}"
require "public_activity/orm/#{orm}"
"PublicActivity::ORM::#{orm.to_s.classify}::#{model}".constantize
end

# Module to be included in ActiveRecord models. Adds required functionality.
module Model
extend ActiveSupport::Concern

included do
include Common
include Deactivatable
include Tracked
include Activist # optional associations by recipient|owner
include Activist # optional associations by recipient|owner
end
end
end
Expand Down
24 changes: 12 additions & 12 deletions lib/public_activity/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,8 @@ def set_public_activity_class_defaults
# @api private
def get_hook(key)
key = key.to_sym
if self.activity_hooks.has_key?(key) and self.activity_hooks[key].is_a? Proc
self.activity_hooks[key]
else
nil
if activity_hooks.key?(key) && activity_hooks[key].is_a?(Proc)
activity_hooks[key]
end
end
end
Expand Down Expand Up @@ -181,7 +179,7 @@ def get_hook(key)
# @since 0.4.0
# @api private
def call_hook_safe(key)
hook = self.get_hook(key)
hook = get_hook(key)
if hook
# provides hook with model and controller
hook.call(self, PublicActivity.get_controller)
Expand Down Expand Up @@ -248,7 +246,8 @@ def call_hook_safe(key)
# @option options [Hash] :params Parameters, see
# {PublicActivity.resolve_value}
def create_activity(*args)
return unless self.public_activity_enabled?
return unless public_activity_enabled?

options = prepare_settings(*args)

if call_hook_safe(options[:key].split('.').last)
Expand All @@ -264,12 +263,13 @@ def create_activity(*args)
#
# @see #create_activity
def create_activity!(*args)
return unless self.public_activity_enabled?
return unless 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)
PublicActivity::Adapter.create_activity!(self, options)
end
end

Expand Down Expand Up @@ -306,7 +306,7 @@ def prepare_settings(*args)
# @private
def prepare_custom_fields(options)
customs = self.class.activity_custom_fields_global.clone
customs.merge!(self.activity_custom_fields) if self.activity_custom_fields
customs.merge!(activity_custom_fields) if activity_custom_fields
customs.merge!(options)
customs.each do |k, v|
customs[k] = PublicActivity.resolve_value(self, v)
Expand All @@ -319,7 +319,7 @@ def prepare_custom_fields(options)
def prepare_parameters(options)
params = {}
params.merge!(self.class.activity_params_global)
params.merge!(self.activity_params) if self.activity_params
params.merge!(activity_params) if activity_params
params.merge!([options.delete(:parameters), options.delete(:params), {}].compact.first)
params.each { |k, v| params[k] = PublicActivity.resolve_value(self, v) }
end
Expand All @@ -329,7 +329,7 @@ def prepare_parameters(options)
# @private
def prepare_relation(name, options)
PublicActivity.resolve_value(self,
(options.has_key?(name) ? options[name] : (
(options.key?(name) ? options[name] : (
self.send("activity_#{name}") || self.class.send("activity_#{name}_global")
)
)
Expand All @@ -343,7 +343,7 @@ def prepare_relation(name, options)
def prepare_key(action, options = {})
(
options[:key] ||
self.activity_key ||
activity_key ||
((self.class.name.underscore.gsub('/', '_') + "." + action.to_s) if action)
).try(:to_s)
end
Expand Down
29 changes: 14 additions & 15 deletions lib/public_activity/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Config
# enabled false
# table_name "activities"
# end
def self.set &block
def self.set(&block)
b = Block.new
b.instance_eval(&block)
instance
Expand All @@ -31,13 +31,13 @@ def self.orm=(orm = nil)

# alias for {#enabled}
# @see #enabled
def self.enabled=(en = nil)
enabled(en)
def self.enabled=(value = nil)
enabled(value)
end

# instance version of {Config#orm}
# @see Config#orm
def orm(orm=nil)
def orm(orm = nil)
self.class.orm(orm)
end

Expand All @@ -49,8 +49,8 @@ def table_name(name = nil)

# instance version of {Config#enabled}
# @see Config#orm
def enabled(en = nil)
self.class.enabled(en)
def enabled(value = nil)
self.class.enabled(value)
end

# Set the ORM for use by PublicActivity.
Expand All @@ -70,12 +70,12 @@ def self.table_name(name = nil)
end
end

def self.enabled(en = nil)
if en.nil?
value = Thread.current[:public_activity_enabled]
value.nil? ? true : value
def self.enabled(value = nil)
if value.nil?
val = Thread.current[:public_activity_enabled]
val.nil? ? true : val
else
Thread.current[:public_activity_enabled] = en
Thread.current[:public_activity_enabled] = value
end
end

Expand All @@ -88,12 +88,11 @@ def orm(orm = nil)

# Decides whether to enable PublicActivity.
# @param en [Boolean] Enabled?
def enabled(en = nil)
@enabled = (en.nil? ? @enabled : en)
def enabled(value = nil)
@enabled = (value.nil? ? @enabled : value)
end

# Sets the table_name
# for the model
# Sets the table_name for the model
def table_name(name = nil)
@table_name = (name.nil? ? @table_name : name)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/public_activity/models/activist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module PublicActivity
module Activist
# Delegates to configured ORM.
def self.included(base)
base.extend PublicActivity::inherit_orm("Activist")
base.extend PublicActivity.inherit_orm('Activist')
end
end
end
2 changes: 1 addition & 1 deletion lib/public_activity/models/activity.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

module PublicActivity
class Activity < inherit_orm("Activity")
class Activity < inherit_orm
end
end
2 changes: 1 addition & 1 deletion lib/public_activity/models/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module PublicActivity
# Loads database-specific routines for use by PublicActivity.
class Adapter < inherit_orm("Adapter")
class Adapter < inherit_orm('Adapter')
end
end
2 changes: 1 addition & 1 deletion lib/public_activity/models/trackable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module PublicActivity
module Trackable
# Delegates to ORM.
def self.included(base)
base.extend PublicActivity::inherit_orm("Trackable")
base.extend PublicActivity.inherit_orm('Trackable')
end
end
end
8 changes: 4 additions & 4 deletions lib/public_activity/orm/active_record.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'active_record'
require_relative 'active_record/activity.rb'
require_relative 'active_record/adapter.rb'
require_relative 'active_record/activist.rb'
require_relative 'active_record/trackable.rb'
require_relative 'active_record/activity'
require_relative 'active_record/adapter'
require_relative 'active_record/activist'
require_relative 'active_record/trackable'
24 changes: 11 additions & 13 deletions lib/public_activity/roles/tracked.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,7 @@ def include_default_actions(options)
update: Update
}

if options[:skip_defaults] == true
return
end
return if options[:skip_defaults] == true

modules = if options[:except]
defaults.except(*options[:except])
Expand All @@ -163,32 +161,32 @@ def include_default_actions(options)
defaults
end

modules.each do |key, value|
modules.each do |_, value|
include value
end
end

def available_options
[:skip_defaults, :only, :except, :on, :owner, :recipient, :params].freeze
%i[skip_defaults only except on owner recipient params].freeze
end

def assign_globals(options)
[:owner, :recipient, :params].each do |key|
if options[key]
self.send("activity_#{key}_global=".to_sym, options.delete(key))
end
%i[owner recipient params].each do |key|
next unless options[key]

send("activity_#{key}_global=".to_sym, options.delete(key))
end
end

def assign_hooks(options)
if options[:on].is_a?(Hash)
self.activity_hooks = options[:on].select {|_, v| v.is_a? Proc}.symbolize_keys
end
return unless options[:on].is_a?(Hash)

self.activity_hooks = options[:on].select { |_, v| v.is_a? Proc }.symbolize_keys
end

def assign_custom_fields(options)
options.except(*available_options).each do |k, v|
self.activity_custom_fields_global[k] = v
activity_custom_fields_global[k] = v
end
end
end
Expand Down

0 comments on commit ae1c60a

Please sign in to comment.