generated from mattbrictson/gem
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feature/alternative-configs: implement alternative configs with module index method Signed-off-by: Dusan <[email protected]> * run formatting Signed-off-by: Dusan <[email protected]> * feature/alternative-configs: cleanup tests Signed-off-by: Dusan <[email protected]> * feature/alternative-configs: update readme Signed-off-by: Dusan <[email protected]> * update deps in gemfile Signed-off-by: Dusan <[email protected]> * update sqlite, reset modules between tests Signed-off-by: Dusan <[email protected]> * remove sqlite? Signed-off-by: Dusan <[email protected]> * use newer sqlite 1.x Signed-off-by: Dusan <[email protected]> * feature/alternative-configs: move version constant to Trackstamps Signed-off-by: Dusan <[email protected]> * rename test context to describe Signed-off-by: Dusan <[email protected]> * fix gemspec Signed-off-by: Dusan <[email protected]> * fix gemspec Signed-off-by: Dusan <[email protected]> * update email Signed-off-by: Dusan <[email protected]> * update email Signed-off-by: Dusan <[email protected]> * rubocop fix Signed-off-by: Dusan <[email protected]> * fix rubocop Signed-off-by: Dusan <[email protected]> * fix rubocop Signed-off-by: Dusan <[email protected]> * fix with model version Signed-off-by: Dusan <[email protected]> --------- Signed-off-by: Dusan <[email protected]>
- Loading branch information
Showing
11 changed files
with
218 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
require "active_support" | ||
require "dry-configurable" | ||
|
||
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/BlockLength, Style/ClassVars | ||
module Trackstamps | ||
module Base | ||
@mixins = ::Concurrent::Map.new | ||
|
||
class Current < ::ActiveSupport::CurrentAttributes | ||
attribute :user | ||
end | ||
|
||
def self.[](instance_name=:default) | ||
@mixins.fetch_or_store(instance_name.to_s) do | ||
Module.new do | ||
@@trackstamps_target_key = instance_name | ||
const_set(:Current, Trackstamps::Base::Current) | ||
|
||
extend Dry::Configurable | ||
extend ActiveSupport::Concern | ||
autoload :VERSION, "trackstamps/reborn/version" | ||
|
||
setting :get_current_user, default: -> { Current.user } | ||
|
||
setting :user_class_name, default: "User".freeze | ||
setting :updater_foreign_key, default: "updated_by_id".freeze | ||
setting :creator_foreign_key, default: "created_by_id".freeze | ||
|
||
def trackstamps_current_user | ||
Trackstamps::Base[@@trackstamps_target_key].config.get_current_user.call | ||
end | ||
|
||
def self.inspect | ||
"Trackstamps::Reborn[:#{@@trackstamps_target_key}]" | ||
end | ||
|
||
def self.included(base) | ||
trackstamps_module_self = self | ||
base.class_eval do | ||
before_save :trackstamps_set_updater | ||
before_create :trackstamps_set_creator | ||
|
||
const_set(:UPDATER_FOREIGN_KEY, trackstamps_module_self.config.updater_foreign_key.dup.freeze) | ||
|
||
private_constant :UPDATER_FOREIGN_KEY | ||
|
||
belongs_to :updater, | ||
class_name: trackstamps_module_self.config.user_class_name, | ||
foreign_key: const_get(:UPDATER_FOREIGN_KEY), | ||
optional: true | ||
|
||
const_set(:CREATOR_FOREIGN_KEY, trackstamps_module_self.config.creator_foreign_key.dup.freeze) | ||
private_constant :CREATOR_FOREIGN_KEY | ||
|
||
belongs_to :creator, | ||
class_name: trackstamps_module_self.config.user_class_name, | ||
foreign_key: const_get(:CREATOR_FOREIGN_KEY), | ||
optional: true | ||
|
||
def trackstamps_set_updater | ||
return unless trackstamps_current_user | ||
|
||
send(:"#{self.class.const_get(:UPDATER_FOREIGN_KEY)}=", trackstamps_current_user.id) | ||
end | ||
|
||
def trackstamps_set_creator | ||
return unless trackstamps_current_user | ||
|
||
send(:"#{self.class.const_get(:CREATOR_FOREIGN_KEY)}=", trackstamps_current_user.id) | ||
end | ||
end | ||
end | ||
|
||
class_methods do | ||
def with_trackstamps | ||
includes(:creator, :updater) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/BlockLength, Style/ClassVars |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,10 @@ | ||
require "active_support" | ||
require "trackstamps/reborn/current" | ||
require "dry-configurable" | ||
require_relative "base" | ||
|
||
module Trackstamps | ||
module Reborn | ||
extend Dry::Configurable | ||
extend ActiveSupport::Concern | ||
autoload :VERSION, "trackstamps/reborn/version" | ||
|
||
setting :get_current_user, default: -> { Trackstamps::Reborn::Current.user } | ||
|
||
setting :user_class_name, default: "User".freeze | ||
setting :updater_foreign_key, default: "updated_by_id".freeze | ||
setting :creator_foreign_key, default: "created_by_id".freeze | ||
|
||
def trackstamps_current_user | ||
Trackstamps::Reborn.config.get_current_user.call | ||
end | ||
|
||
included do | ||
before_save :trackstamps_set_updater | ||
before_create :trackstamps_set_creator | ||
|
||
const_set(:UPDATER_FOREIGN_KEY, Trackstamps::Reborn.config.updater_foreign_key.dup.freeze) | ||
|
||
private_constant :UPDATER_FOREIGN_KEY | ||
|
||
belongs_to :updater, | ||
class_name: Trackstamps::Reborn.config.user_class_name, | ||
foreign_key: const_get(:UPDATER_FOREIGN_KEY), | ||
optional: true | ||
|
||
const_set(:CREATOR_FOREIGN_KEY, Trackstamps::Reborn.config.creator_foreign_key.dup.freeze) | ||
private_constant :CREATOR_FOREIGN_KEY | ||
|
||
belongs_to :creator, | ||
class_name: Trackstamps::Reborn.config.user_class_name, | ||
foreign_key: const_get(:CREATOR_FOREIGN_KEY), | ||
optional: true | ||
|
||
def trackstamps_set_updater | ||
return unless trackstamps_current_user | ||
|
||
send("#{self.class.const_get(:UPDATER_FOREIGN_KEY)}=", trackstamps_current_user.id) | ||
end | ||
|
||
def trackstamps_set_creator | ||
return unless trackstamps_current_user | ||
|
||
send("#{self.class.const_get(:CREATOR_FOREIGN_KEY)}=", trackstamps_current_user.id) | ||
end | ||
end | ||
|
||
class_methods do | ||
def with_trackstamps | ||
includes(:creator, :updater) | ||
end | ||
Reborn = Trackstamps::Base[:default] | ||
Reborn.module_eval do | ||
def self.[](instance_name) | ||
Trackstamps::Base[instance_name] | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module Trackstamps | ||
VERSION = "0.1.1".freeze | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
RSpec.describe Trackstamps::Reborn do | ||
describe "changing defaults is reflected in itself" do | ||
before do | ||
Trackstamps::Reborn.config.user_class_name = "Account" | ||
end | ||
|
||
it "is reflected" do | ||
expect(Trackstamps::Reborn[:default].config.user_class_name).to eq("Account") | ||
end | ||
end | ||
|
||
describe "changing another does not interfere with user class name" do | ||
before do | ||
Trackstamps::Reborn.config.user_class_name = "Default" | ||
Trackstamps::Reborn[:alternative].config.user_class_name = "Alternative" | ||
end | ||
|
||
it "default is fine" do | ||
expect(Trackstamps::Reborn[:default].config.user_class_name).to eq("Default") | ||
end | ||
|
||
it "alternative is fine" do | ||
expect(Trackstamps::Reborn[:alternative].config.user_class_name).to eq("Alternative") | ||
end | ||
end | ||
|
||
describe "changing another does not interfere with updater foreign key" do | ||
before do | ||
Trackstamps::Reborn.config.updater_foreign_key = "updater_default_id" | ||
Trackstamps::Reborn[:alternative].config.updater_foreign_key = "updater_alternative_id" | ||
end | ||
|
||
it "default is fine" do | ||
expect(Trackstamps::Reborn[:default].config.updater_foreign_key).to eq("updater_default_id") | ||
end | ||
|
||
it "alternative is fine" do | ||
expect(Trackstamps::Reborn[:alternative].config.updater_foreign_key).to eq("updater_alternative_id") | ||
end | ||
end | ||
|
||
describe "changing another does not interfere with creator foreign key" do | ||
before do | ||
Trackstamps::Reborn.config.creator_foreign_key = "creator_default_id" | ||
Trackstamps::Reborn[:alternative].config.creator_foreign_key = "creator_alternative_id" | ||
end | ||
|
||
it "default is fine" do | ||
expect(Trackstamps::Reborn[:default].config.creator_foreign_key).to eq("creator_default_id") | ||
end | ||
|
||
it "alternative is fine" do | ||
expect(Trackstamps::Reborn[:alternative].config.creator_foreign_key).to eq("creator_alternative_id") | ||
end | ||
end | ||
|
||
describe "changing another does not interfere with get current user proc" do | ||
before do | ||
Trackstamps::Reborn.config.get_current_user = -> { "default" } | ||
Trackstamps::Reborn[:alternative].config.get_current_user = -> { "alternative" } | ||
end | ||
|
||
it "default is fine" do | ||
expect(Trackstamps::Reborn[:default].config.get_current_user.call).to eq("default") | ||
end | ||
|
||
it "alternative is fine" do | ||
expect(Trackstamps::Reborn[:alternative].config.get_current_user.call).to eq("alternative") | ||
end | ||
end | ||
|
||
it "instance is only created once" do | ||
instance_1 = Trackstamps::Reborn[:alternative] | ||
instance_2 = Trackstamps::Reborn[:alternative] | ||
|
||
expect(instance_1).to eq(instance_2) | ||
end | ||
|
||
it "different instances do not match" do | ||
instance_1 = Trackstamps::Reborn[:default] | ||
instance_2 = Trackstamps::Reborn[:alternative] | ||
|
||
expect(instance_1).not_to eq(instance_2) | ||
end | ||
|
||
it "inspect works properly" do | ||
instance = Trackstamps::Reborn[:inspected] | ||
expect(instance.inspect).to eq("Trackstamps::Reborn[:inspected]") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
require_relative "lib/trackstamps/reborn/version" | ||
require_relative "lib/trackstamps/version" | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = "trackstamps-reborn" | ||
spec.version = Trackstamps::Reborn::VERSION | ||
spec.version = Trackstamps::VERSION | ||
spec.authors = ["Dušan"] | ||
spec.email = ["[email protected]"] | ||
spec.email = ["[email protected]"] | ||
|
||
spec.summary = "" | ||
spec.homepage = "https://github.com/uvera/trackstamps-reborn" | ||
|