This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from peresvetjke/feature/UCMS-7655
[UCMS-7655] Add EntitiesLogDump, FlexibleBoolean, UrlFormatter, CiFor…
- Loading branch information
Showing
17 changed files
with
615 additions
and
28 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,8 @@ | ||
--- | ||
ru: | ||
unity_utils: | ||
errors: | ||
blank_url: Ссылка не может быть пустой | ||
invalid_url_format: Неправильный формат ссылки | ||
blank_host: Хост не может быть пустым | ||
invalid_protocol: Ссылка должна начинаться с http(s) |
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,81 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'active_support/concern' | ||
|
||
module Unity | ||
module Modules | ||
# You could use this module for service objects | ||
# class YourServiceObject | ||
# include Helpers::EntitiesLogDump | ||
# | ||
# You can get array of logged entities by custom attributes like that: | ||
# def topic_query | ||
# @topic_query ||= Topic.first(2) | ||
# end | ||
# | ||
# def foo | ||
# dump_entities(topic_query, attributes: [:id, :headline]) | ||
# end | ||
# Result: [{ "id" => 1, "headline" => "foo" }, { "id" => 2, "headline" => "bar" }] | ||
# | ||
module EntitiesLogDump | ||
extend ::ActiveSupport::Concern | ||
|
||
# @param entities [ActiveRecord::Relation,Array<Mongoid::Document>] | ||
# @param [Hash] options | ||
# @option options [Array<Symbol>] :attributes | ||
# @option options [Integer] :batch_size | ||
# @return [Array<Hash>] | ||
def dump_entities(entities, options = {}) | ||
attributes = options[:attributes] || all_attributes(entities.first) | ||
batch_size = options[:batch_size] || 100 | ||
|
||
dump_entities = [] | ||
entities.each_slice(batch_size) do |sliced_entities| | ||
sliced_entities.each do |entity| | ||
dump_entities.append(dump_entity(entity, attributes: attributes)) | ||
end | ||
end | ||
|
||
dump_entities | ||
end | ||
|
||
# @param entity [ApplicationRecord,Mongoid::Document] | ||
# @param [Hash] options | ||
# @option options [Array<Symbol>] :attributes | ||
# @return [Hash] | ||
def dump_entity(entity, options = {}) | ||
attributes = options[:attributes] || all_attributes(entity) | ||
attributes.map { |a| { a => try_array(entity.send(a)) } }.inject(:merge).deep_stringify_keys | ||
end | ||
|
||
# @param entity [ApplicationRecord,Mongoid::Document] | ||
# @return [Array<String,Symbol>] | ||
def all_attributes(entity) | ||
return entity.class.attribute_names if object_is_a?(entity, 'ApplicationRecord') | ||
return entity.fields.keys if object_is_a?(entity, 'Mongoid::Document') | ||
|
||
raise ArgumentError, "Can't determine full attributes list for this kind of entity." | ||
end | ||
|
||
private | ||
|
||
# @item [Object] | ||
# @return | ||
def try_array(item) | ||
return item.map { |i| i.try(:attributes) || i } if item.is_a?(Array) | ||
return item.map(&:attributes) if item.try(:any?) { |obj| object_is_a?(obj, 'ActiveRecord::Base') } | ||
|
||
item | ||
end | ||
|
||
# @param obj [Object] | ||
# @klass_name [String] | ||
# @return [Boolean] | ||
def object_is_a?(obj, klass_name) | ||
ancestors = obj.class.ancestors.map(&:to_s) | ||
ancestors.include?(klass_name) | ||
end | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'active_support/concern' | ||
|
||
module Unity | ||
module Modules | ||
# Concern to make easier work with virtual boolean attributes. | ||
# Allows always store a boolean value in a pseudo attribute | ||
# (pass string or boolean, attribute will boolean value type always). | ||
# | ||
# Overrides target attribute setter method by adding a | ||
# comparison of the boolean attribute value with the 'true' string. | ||
# | ||
# Usage example: | ||
# flexible_boolean :is_reactable | ||
# | ||
module FlexibleBoolean | ||
extend ::ActiveSupport::Concern | ||
|
||
class_methods do | ||
# @param attribute [Symbol, String] | ||
# @return [Symbol] defined method name | ||
def flexible_boolean(attribute) | ||
define_method(:"#{attribute}=") { |v| super(v.to_s == 'true') } | ||
end | ||
end | ||
end | ||
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
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
Oops, something went wrong.