tramway-core 1.17.2.2 → 1.17.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3c612a0122da3218a86e61b973beff51f378e14352fd424489130039694f82f8
4
- data.tar.gz: 07d47a9b6e995469f7b500e34b21f705cc24ce1e4aede1e946ace36f5ddebb28
3
+ metadata.gz: e25693262017e7eb7b500db8ed733e26ed5c841f4e5eca94b036f422114c8f8e
4
+ data.tar.gz: 9cc1274352800f545610e01d2c02485c3ef2837bd2d413041818462682496e04
5
5
  SHA512:
6
- metadata.gz: 8b275c112e32864152637a34c283677de289fc2fb5e79123fd15f149656e0d3741c1b7768e06d441004927e4fe5db0f4af2a744a55e6fdc58fe210227e1b7ce7
7
- data.tar.gz: 350ace536d11005778f241370a3179f5f51f9078d8822ca4e80955aeb0ffb93edbafc64e55d9635d0cba4e4cc4ef8b3fd6f3aa205a7171542b25e6500c82d281
6
+ metadata.gz: 796e723d4c64a741d72b2d25a769e1344d895805dda3ba55da9c6f4161252a416ade82f2439217599a60605c6d29e6292be4b165a56f0c26343eeeba6ca9ce85
7
+ data.tar.gz: 9991a6ecf790a5ce4ed3b0064d3cc2392336359bcf095416b91a9ee6d152e19d1487826ab9a85d7c1df4913a426375128955c602b1501ff5b273319ca49db211
@@ -1,18 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Tramway
4
- module Core
5
- class ApplicationController < ActionController::Base
6
- before_action :application
7
- before_action :load_extensions
3
+ class Tramway::Core::ApplicationController < ActionController::Base
4
+ before_action :application
5
+ before_action :load_extensions
8
6
 
9
- def application
10
- @application = ::Tramway::Core.application_object
11
- end
7
+ def application
8
+ @application = ::Tramway::Core.application_object
9
+ end
12
10
 
13
- def load_extensions
14
- ::Tramway::Extensions.load if defined? ::Tramway::Extensions
15
- end
16
- end
11
+ def load_extensions
12
+ ::Tramway::Extensions.load if defined? ::Tramway::Extensions
17
13
  end
18
14
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'tramway/error'
4
+ require 'tramway/helpers/class_name_helpers'
4
5
 
5
6
  class Tramway::Core::ApplicationDecorator
6
7
  include ActionView::Helpers
@@ -8,6 +9,8 @@ class Tramway::Core::ApplicationDecorator
8
9
  include ::FontAwesome5::Rails::IconHelper
9
10
  include ::Tramway::Core::CopyToClipboardHelper
10
11
  include ::Tramway::Core::Associations::ObjectHelper
12
+ include ::Tramway::Core::Attributes::ViewHelper
13
+ include ::Tramway::ClassNameHelpers
11
14
 
12
15
  def initialize(object)
13
16
  @object = object
@@ -18,8 +21,10 @@ class Tramway::Core::ApplicationDecorator
18
21
  end
19
22
 
20
23
  def title
21
- error = Tramway::Error.new(plugin: :core, method: :title, message: "Please, implement `title` method in a #{self.class} or #{object.class}")
22
- raise error.message
24
+ Tramway::Error.raise_error(
25
+ :tramway, :core, :application_decorator, :title, :please_implement_title,
26
+ class_name: self.class, object_class: object.class
27
+ )
23
28
  end
24
29
 
25
30
  delegate :id, to: :object
@@ -69,12 +74,7 @@ class Tramway::Core::ApplicationDecorator
69
74
  if object.try :file
70
75
  object.file.url
71
76
  else
72
- error = Tramway::Error.new(
73
- plugin: :core,
74
- method: :link,
75
- message: "Method `link` uses `file` attribute of the decorated object. If decorated object doesn't contain `file`, you shouldn't use `link` method."
76
- )
77
- raise error.message
77
+ Tramway::Error.raise_error :tramway, :core, :application_decorator, :link, :method_link_uses_file_attribute
78
78
  end
79
79
  end
80
80
 
@@ -97,25 +97,12 @@ class Tramway::Core::ApplicationDecorator
97
97
  def attributes
98
98
  object.attributes.reduce({}) do |hash, attribute|
99
99
  if attribute[0].in? RESERVED_WORDS
100
- error = Tramway::Error.new(
101
- plugin: :core,
102
- method: :attributes,
103
- message: "Method `#{attribute[0]}` is reserved word. Please, create or delegate method in #{self.class.name} with another name."
100
+ Tramway::Error.raise_error(
101
+ :tramway, :core, :application_decorator, :attributes, :method_is_reserved_word,
102
+ attribute_name: attribute[0], class_name: self.class.name
104
103
  )
105
- raise error.message
106
- end
107
- value = try(attribute[0]) ? send(attribute[0]) : object.send(attribute[0])
108
- if attribute[0].to_s.in? object.class.state_machines.keys.map(&:to_s)
109
- hash.merge! attribute[0] => state_machine_view(object, attribute[0])
110
- elsif value.class.in? [ActiveSupport::TimeWithZone, DateTime, Time]
111
- hash.merge! attribute[0] => datetime_view(attribute[1])
112
- elsif value.class.superclass == ApplicationUploader
113
- hash.merge! attribute[0] => image_view(object.send(attribute[0]))
114
- elsif value.is_a? Enumerize::Value
115
- hash.merge! attribute[0] => enumerize_view(value)
116
- else
117
- hash.merge! attribute[0] => value
118
104
  end
105
+ hash.merge! attribute[0] => build_viewable_value(object, attribute)
119
106
  end
120
107
  end
121
108
 
@@ -16,7 +16,7 @@ module Tramway::Core::Associations::ClassHelper
16
16
  end
17
17
 
18
18
  define_method "add_#{association_name}_form" do
19
- "Admin::#{object.class.to_s.pluralize}::Add#{association_name.to_s.camelize.singularize}Form".constantize.new object
19
+ add_association_form_class_name(object, association_name).new object
20
20
  end
21
21
  end
22
22
 
@@ -29,19 +29,12 @@ module Tramway::Core::Associations::ClassHelper
29
29
  def define_main_association_method(association_name, decorator)
30
30
  define_method association_name do
31
31
  association = object.class.reflect_on_association(association_name)
32
- if association.nil?
33
- error = Tramway::Error.new(plugin: :core, method: :decorate_association, message: "Model #{object.class} does not have association named `#{association_name}`")
34
- raise error.message
35
- end
36
- decorator_class_name = decorator || "#{class_name(association).to_s.singularize}Decorator".constantize
37
- if association.class.in? [ActiveRecord::Reflection::HasManyReflection, ActiveRecord::Reflection::HasAndBelongsToManyReflection]
38
- return object.send(association_name).active.map do |association_object|
39
- decorator_class_name.decorate association_object
40
- end
41
- end
42
- if association.class == ActiveRecord::Reflection::BelongsToReflection
43
- return decorator_class_name.decorate object.send association_name
32
+ check_association object, association_name, association
33
+ decorator_class_name = decorator || decorator_class_name(class_name(association))
34
+ if association_type(association).in? %i[has_many has_and_belongs_to_many]
35
+ return associations_collection(object, association_name, decorator_class_name)
44
36
  end
37
+ return decorator_class_name.decorate object.send association_name if association_type(association) == :belongs_to
45
38
  end
46
39
  end
47
40
  end
@@ -6,10 +6,36 @@ module Tramway::Core::Associations::ObjectHelper
6
6
  object.send(association.name).class
7
7
  else
8
8
  unless association.options[:class_name]
9
- error = Tramway::Error.new(plugin: :core, method: :decorate_association, message: "Please, specify `#{association.name}` association class_name in #{object.class} model. For example: `has_many :#{association.name}, class_name: '#{association.name.to_s.singularize.camelize}'`")
10
- raise error.message
9
+ Tramway::Error.raise_error(
10
+ :tramway, :core, :associations, :object_helper, :please_specify_association_name,
11
+ association_name: association.name, object_class: object.class,
12
+ association_class_name: association.name.to_s.singularize.camelize
13
+ )
11
14
  end
12
15
  association.options[:class_name]
13
16
  end
14
17
  end
18
+
19
+ def check_association(object, association_name, association)
20
+ return unless association.nil?
21
+
22
+ Tramway::Error.raise_error(
23
+ :tramway, :core, :associations, :class_helper, :model_does_not_have_association,
24
+ object_class: object.class, association_name: association_name
25
+ )
26
+ end
27
+
28
+ def association_type(association)
29
+ association.class.to_s.split('::').last.sub(/Reflection$/, '').underscore.to_sym
30
+ end
31
+
32
+ def associations_collection(object, association_name, decorator_class_name)
33
+ object.send(association_name).active.map do |association_object|
34
+ decorator_class_name.decorate association_object
35
+ end
36
+ end
37
+
38
+ def add_association_form_class_name(object, association_name)
39
+ "Admin::#{object.class.to_s.pluralize}::Add#{association_name.to_s.camelize.singularize}Form".constantize
40
+ end
15
41
  end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tramway::Core::Attributes::ViewHelper
4
+ def build_viewable_value(object, attribute)
5
+ value = try(attribute[0]) ? send(attribute[0]) : object.send(attribute[0])
6
+ return state_machine_view(object, attribute[0]) if state_machine? object, attribute[0]
7
+
8
+ view_by_value object, value, attribute
9
+ end
10
+
11
+ def state_machine?(object, attribute_name)
12
+ attribute_name.to_s.in? object.class.state_machines.keys.map(&:to_s)
13
+ end
14
+
15
+ def view_by_value(object, value, attribute)
16
+ if value.class.in? [ActiveSupport::TimeWithZone, DateTime, Time]
17
+ datetime_view(attribute[1])
18
+ elsif value.class.superclass == ApplicationUploader
19
+ image_view(object.send(attribute[0]))
20
+ elsif value.is_a? Enumerize::Value
21
+ enumerize_view(value)
22
+ else
23
+ value
24
+ end
25
+ end
26
+ end
@@ -13,37 +13,58 @@ module Tramway::Core::Concerns::AttributesDecoratorHelper
13
13
  object.send "human_#{attribute_name}_name"
14
14
  end
15
15
 
16
+ BASE64_REGEXP = %r{^(?:[a-zA-Z0-9+/]{4})*(?:|(?:[a-zA-Z0-9+/]{3}=)|
17
+ (?:[a-zA-Z0-9+/]{2}==)|(?:[a-zA-Z0-9+/]{1}===))$}x.freeze
18
+
16
19
  def image_view(original, thumb: nil, filename: nil)
17
- if original.present?
18
- thumb ||= original.is_a?(CarrierWave::Uploader::Base) ? original.small : nil
19
- filename ||= original.is_a?(CarrierWave::Uploader::Base) ? original.path&.split('/')&.last : nil
20
- src_thumb = if thumb&.is_a?(CarrierWave::Uploader::Base)
21
- thumb.url
22
- elsif thumb&.match(%r{^(?:[a-zA-Z0-9+/]{4})*(?:|(?:[a-zA-Z0-9+/]{3}=)|(?:[a-zA-Z0-9+/]{2}==)|(?:[a-zA-Z0-9+/]{1}===))$})
23
- "data:image/jpeg;base64,#{thumb}"
24
- else
25
- thumb
26
- end
27
- src_original = if original.is_a?(CarrierWave::Uploader::Base)
28
- original.url
29
- elsif original.match(%r{^(?:[a-zA-Z0-9+/]{4})*(?:|(?:[a-zA-Z0-9+/]{3}=)|(?:[a-zA-Z0-9+/]{2}==)|(?:[a-zA-Z0-9+/]{1}===))$})
30
- "data:image/jpeg;base64,#{original}"
31
- else
32
- original
33
- end
34
- content_tag(:div) do
35
- begin
36
- concat image_tag src_thumb || src_original
37
- rescue NoMethodError => e
38
- error = Tramway::Error.new plugin: :core, method: :image_view, message: "You should mount PhotoUploader to your model. Just add `mount_uploader \#{attribute_name}, PhotoUploader` to your model. #{e.message}"
39
- raise error.message
40
- end
41
- concat link_to(fa_icon(:download), src_original, class: 'btn btn-success', download: filename) if filename
42
- end
20
+ return unless original.present?
21
+
22
+ filename ||= build_filename(original)
23
+ content_tag(:div) do
24
+ build_div_content src_original(original), src_thumb(original, thumb), filename || build_filename(original)
43
25
  end
44
26
  end
45
27
 
46
28
  def enumerize_view(value)
47
29
  value.text
48
30
  end
31
+
32
+ private
33
+
34
+ def src_thumb(original, thumb)
35
+ thumb ||= original.is_a?(CarrierWave::Uploader::Base) ? original.small : nil
36
+ if thumb&.is_a?(CarrierWave::Uploader::Base)
37
+ thumb.url
38
+ elsif thumb&.match(BASE64_REGEXP)
39
+ "data:image/jpeg;base64,#{thumb}"
40
+ else
41
+ thumb
42
+ end
43
+ end
44
+
45
+ def src_original(original)
46
+ if original.is_a?(CarrierWave::Uploader::Base)
47
+ original.url
48
+ elsif original.match(BASE64_REGEXP)
49
+ "data:image/jpeg;base64,#{original}"
50
+ else
51
+ original
52
+ end
53
+ end
54
+
55
+ def build_filename(original)
56
+ original.is_a?(CarrierWave::Uploader::Base) ? original.path&.split('/')&.last : nil
57
+ end
58
+
59
+ def build_div_content(original, thumb, filename)
60
+ begin
61
+ concat image_tag src_thumb(original, thumb) || src_original(original)
62
+ rescue NoMethodError => e
63
+ Tramway::Error.raise_error(
64
+ :tramway, :core, :concerns, :attributes_decorator_helper, :you_should_mount_photo_uploader,
65
+ message: e.message, attribute_name: attribute_name
66
+ )
67
+ end
68
+ concat link_to(fa_icon(:download), src_original(original), class: 'btn btn-success', download: filename) if filename
69
+ end
49
70
  end
@@ -54,7 +54,7 @@ class Tramway::Core::ApplicationForm < ::Reform::Form
54
54
 
55
55
  if options&.dig(:polymorphic)
56
56
  hash.merge! association => @@model_class.send("#{association}_type").values
57
- elsif options.present?
57
+ elsif options
58
58
  hash.merge!(association => (options[:class_name] || association.to_s.camelize).constantize)
59
59
  end
60
60
  hash
@@ -30,6 +30,10 @@ module Tramway::Core
30
30
  end
31
31
  end
32
32
 
33
+ def root
34
+ File.dirname __dir__
35
+ end
36
+
33
37
  attr_reader :application
34
38
  end
35
39
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Tramway
4
4
  module Core
5
- VERSION = '1.17.2.2'
5
+ VERSION = '1.17.2.3'
6
6
  end
7
7
  end
@@ -19,10 +19,10 @@ class Tramway::Error < RuntimeError
19
19
 
20
20
  class << self
21
21
  def raise_error(*coordinates, **options)
22
- @errors ||= YAML.load_file('..', '..', 'yaml', 'errors.yml').with_indifferent_access
22
+ @errors ||= YAML.load_file("#{Tramway::Core.root}/yaml/errors.yml").with_indifferent_access
23
23
  error = @errors.dig(*coordinates)
24
24
  options.each do |pair|
25
- error.gsub!("%{#{pair[0]}}", pair[1])
25
+ error.gsub!("%{#{pair[0]}}", pair[1].to_s)
26
26
  end
27
27
  raise error
28
28
  end
@@ -0,0 +1,35 @@
1
+ tramway:
2
+ collections:
3
+ helper:
4
+ collection_list_by:
5
+ there_no_such_collection: "There no such collection named %{name_camelize}. Please create class with self method `list` and extended of `Tramway::Collection`. You should reload your server after creating this collection."
6
+ core:
7
+ title_helper:
8
+ title:
9
+ you_should_set_tramway_core_application: "You should set Tramway::Core::Application class using `::Tramway::Core.initialize_application model_class: #{model_class_name}` in config/initializers/tramway.rb OR maybe you don't have any records of application model"
10
+ application_form:
11
+ initialize:
12
+ polymorphic_class_is_nil: "Polymorphic association class is nil. Maybe, you should write `assocation #{association_name}` after `properties #{association_name}_id, #{association_name}_type`"
13
+ validates:
14
+ you_need_to_set_model_class: 'You need to set `model_class` name while using validations. Just write `self.model_class = YOUR_MODEL_NAME` in the class area.'
15
+ submit:
16
+ looks_like_you_have_method: "Looks like you have method `%{method_name}` in %{model_class}. You should rename it or rename property in %{class_name}"
17
+ params_should_not_be_nil: "ApplicationForm::Params should not be nil"
18
+ model_class:
19
+ there_is_not_model_class: "There is not model class name for %{name}. Should be %{model_class_name} or you can use another class to initialize form object or just initialize form with object."
20
+ concerns:
21
+ attribute_decorator_helper:
22
+ you_should_mount_photo_uploader: "You should mount PhotoUploader to your model. Just add `mount_uploader %{attribute_name}, PhotoUploader` to your model. %{message}"
23
+ associations:
24
+ object_helper:
25
+ please_specify_association_name: "Please, specify `%{association_name}` association class_name in %{object_class} model. For example: `has_many :%{association_name}, class_name: '%{association_class_name}'`"
26
+ class_helper:
27
+ model_does_not_have_association: "Model %{object_class} does not have association named `%{association_name}`"
28
+ application_decorator:
29
+ attributes:
30
+ method_is_reserved_word: "Method `%{attribute_name}` is reserved word. Please, create or delegate method in %{class_name} with another name."
31
+ title:
32
+ please_implement_title: "Please, implement `title` method in a %{class_name} or %{object_class}"
33
+ link:
34
+ method_link_uses_file_attribute: "Method `link` uses `file` attribute of the decorated object. If decorated object doesn't contain `file`, you shouldn't use `link` method."
35
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tramway-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.17.2.2
4
+ version: 1.17.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Kalashnikov
@@ -358,6 +358,7 @@ files:
358
358
  - app/decorators/tramway/core/application_decorator.rb
359
359
  - app/decorators/tramway/core/associations/class_helper.rb
360
360
  - app/decorators/tramway/core/associations/object_helper.rb
361
+ - app/decorators/tramway/core/attributes/view_helper.rb
361
362
  - app/decorators/tramway/core/concerns/attributes_decorator_helper.rb
362
363
  - app/decorators/tramway/core/delegating/class_helper.rb
363
364
  - app/forms/tramway/core/application_form.rb
@@ -422,6 +423,7 @@ files:
422
423
  - lib/tramway/error.rb
423
424
  - lib/tramway/helpers/class_name_helpers.rb
424
425
  - lib/validators/presence_validator.rb
426
+ - lib/yaml/errors.yml
425
427
  homepage: https://github.com/purple-magic/tramway-core
426
428
  licenses:
427
429
  - MIT