From ae1c60a766efec85ae117387a94d67630c90f51c Mon Sep 17 00:00:00 2001 From: ur5us Date: Sat, 20 Jan 2024 10:20:09 +1300 Subject: [PATCH] Refactor and fix linter issues --- lib/public_activity.rb | 7 +++--- lib/public_activity/common.rb | 24 ++++++++++---------- lib/public_activity/config.rb | 29 ++++++++++++------------ lib/public_activity/models/activist.rb | 2 +- lib/public_activity/models/activity.rb | 2 +- lib/public_activity/models/adapter.rb | 2 +- lib/public_activity/models/trackable.rb | 2 +- lib/public_activity/orm/active_record.rb | 8 +++---- lib/public_activity/roles/tracked.rb | 24 +++++++++----------- 9 files changed, 49 insertions(+), 51 deletions(-) diff --git a/lib/public_activity.rb b/lib/public_activity.rb index fecab852..0d6ca4f0 100644 --- a/lib/public_activity.rb +++ b/lib/public_activity.rb @@ -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 diff --git a/lib/public_activity/common.rb b/lib/public_activity/common.rb index a75687e6..fcf0d381 100644 --- a/lib/public_activity/common.rb +++ b/lib/public_activity/common.rb @@ -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 @@ -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) @@ -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) @@ -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 @@ -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) @@ -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 @@ -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") ) ) @@ -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 diff --git a/lib/public_activity/config.rb b/lib/public_activity/config.rb index 26749772..203aec13 100644 --- a/lib/public_activity/config.rb +++ b/lib/public_activity/config.rb @@ -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 @@ -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 @@ -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. @@ -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 @@ -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 diff --git a/lib/public_activity/models/activist.rb b/lib/public_activity/models/activist.rb index 1c3049fb..bf229724 100644 --- a/lib/public_activity/models/activist.rb +++ b/lib/public_activity/models/activist.rb @@ -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 diff --git a/lib/public_activity/models/activity.rb b/lib/public_activity/models/activity.rb index 7d440b39..d2bf2121 100644 --- a/lib/public_activity/models/activity.rb +++ b/lib/public_activity/models/activity.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true module PublicActivity - class Activity < inherit_orm("Activity") + class Activity < inherit_orm end end diff --git a/lib/public_activity/models/adapter.rb b/lib/public_activity/models/adapter.rb index 9d8b9886..bcb1ff18 100644 --- a/lib/public_activity/models/adapter.rb +++ b/lib/public_activity/models/adapter.rb @@ -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 diff --git a/lib/public_activity/models/trackable.rb b/lib/public_activity/models/trackable.rb index 6b147ed4..3cdb74de 100644 --- a/lib/public_activity/models/trackable.rb +++ b/lib/public_activity/models/trackable.rb @@ -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 diff --git a/lib/public_activity/orm/active_record.rb b/lib/public_activity/orm/active_record.rb index 72082a93..77a7ab9e 100644 --- a/lib/public_activity/orm/active_record.rb +++ b/lib/public_activity/orm/active_record.rb @@ -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' diff --git a/lib/public_activity/roles/tracked.rb b/lib/public_activity/roles/tracked.rb index d4449240..eccc4073 100644 --- a/lib/public_activity/roles/tracked.rb +++ b/lib/public_activity/roles/tracked.rb @@ -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]) @@ -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