tramway-core 1.18.0.1 → 1.18.1.1

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: bc1dd51b28f3431c8df5cd27f8350d7dc2b4bedcf7a811c48130a9eb8d1d207b
4
- data.tar.gz: fe2291fcb3bc27d9de9ebe3753ea493ef99a19ebb49db88fcf6e3b9917b2c6be
3
+ metadata.gz: ff863d7c75ba76a10d1f9c751b9710474a16b0cd126a6974224d8cbcb26d9dd4
4
+ data.tar.gz: b890fdbb1e3a33e6dc9cac04ca8cc3d91a5c240a9337c4313b576faf44f47c7b
5
5
  SHA512:
6
- metadata.gz: 63428cee65707e287d8abb8e2d110f1b83342213a14d43f77bebb20530a9fb0765bd81d4659360a258f59d6597e32120d415ee80a508c9abb4bc49681d357b20
7
- data.tar.gz: 3d597d30be2064ece9c9d7520964445868349ab8c924400f5f944fe18abade3efb673a1026ebc5dc576b178900ee4831682aff7a51e38f23c8bfd056cc2b03e3
6
+ metadata.gz: 2ae3500906ffdc568ce881fad3c6581958bfcb32d5f145242f93eac7d6710a05a0d549e734ae3542040366cc75e584846f5100bdcada8e29e9529975f6e194af
7
+ data.tar.gz: f6be135129633829f2585831fc2576a566618f023f1bc4ab15f7b96e35437c9fddc668c6fa14a02d273d3c46e8c5ee5be4149946f6df8a679fe5628c8a26d398
data/README.md CHANGED
@@ -34,7 +34,31 @@ Rails.application.config.assets.precompile += %w( *.jpg *.png *.js )
34
34
  ```
35
35
  # Usage
36
36
 
37
- ## Decorators
37
+ ## Tramway::Core::ApplicationRecord
38
+
39
+ ### uploader
40
+
41
+ Tramway use [carrierwave](https://github.com/carrierwaveuploader/carrierwave) for file uploading by default. To mount uploader you should use `uploader` method
42
+
43
+ Interface: `uploader(attribute_name, uploader_name, **options)`
44
+
45
+ * attribute_name - ActiveRecord attribute to mount uploader
46
+ * uploader_name - **short** uploader name. You need to connect uploaders which are compatible with Tramway. Available uploaders:
47
+ * :photo - you can see it [here](https://github.com/Purple-Magic/tramway-core/blob/develop/app/uploaders/photo_uploader.rb)
48
+ * :file - you can see it [here](https://github.com/Purple-Magic/tramway-core/blob/develop/app/uploaders/file_uploader.rb)
49
+ * options - you are available to set options for uploaders exactly for this model. Available options:
50
+ * versions - **only for :photo**. Set needed versions for file to be cropped. If empty - 0 zero versions will be used. All versions you can see [here](https://github.com/Purple-Magic/tramway-core/blob/develop/app/uploaders/photo_uploader.rb)
51
+ * extensions - whitelist of file extensions. If empty will be used default whitelist from the uploaders (links above)
52
+
53
+ Example:
54
+
55
+ ```ruby
56
+ class User < Tramway::Core::ApplicationRecord
57
+ uploader :avatar, :photo, version: [ :small, :medium ], extensions: [ :jpg, :jpeg ]
58
+ end
59
+ ```
60
+
61
+ ## Tramway::Core::ApplicationDecorator
38
62
  ### Associations
39
63
 
40
64
  Your can decorate association models. Supporting all types of association
@@ -157,7 +181,7 @@ copy_to_clipboard "some_id" # some_id is HTML id of element. Content of this ele
157
181
  #### 1. Generate model that you to use. We create Organization, for example
158
182
 
159
183
  ```shell
160
- rails g model organization name:text public_name:text tagline:text address:text phone:text coordinates:point, state: text, favicon:text # remember! State field is required, if you use tramway-admin
184
+ rails g model organization name:text public_name:text tagline:text address:text phone:text coordinates:point state: text favicon:text # remember! State field is required, if you use tramway-admin
161
185
  rails db:migrate
162
186
  ```
163
187
 
@@ -29,12 +29,14 @@ 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
+ return if association_type(association) == :has_one && object.send(association_name) == nil
33
+
32
34
  check_association object, association_name, association
33
35
  decorator_class_name = decorator || decorator_class_name(class_name(association))
34
36
  if association_type(association).in? %i[has_many has_and_belongs_to_many]
35
37
  return associations_collection(object, association_name, decorator_class_name)
36
38
  end
37
- return decorator_class_name.decorate object.send association_name if association_type(association) == :belongs_to
39
+ return decorator_class_name.decorate object.send association_name if association_type(association).in? [ :belongs_to, :has_one ]
38
40
  end
39
41
  end
40
42
  end
@@ -36,6 +36,15 @@ module Tramway::Core::Associations::ObjectHelper
36
36
  end
37
37
 
38
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
39
+ form_class = "Admin::#{object.class.to_s.pluralize}::Add#{association_name.to_s.camelize.singularize}Form"
40
+
41
+ begin
42
+ form_class.constantize
43
+ rescue
44
+ Tramway::Error.raise_error(
45
+ :tramway, :core, :associations, :object_helper, :habtm_add_class_not_defined,
46
+ class: form_class, association_name: association_name
47
+ )
48
+ end
40
49
  end
41
50
  end
@@ -5,19 +5,12 @@ module Tramway::Core::ApplicationForms::PropertiesObjectHelper
5
5
  @form_properties = args
6
6
  end
7
7
 
8
- def form_properties_additional(**args)
9
- @form_properties_additional = args
10
- end
11
-
12
8
  def properties
13
- return @form_properties if @form_properties
14
-
9
+ @form_properties ||= {}
15
10
  yaml_config_file_path = Rails.root.join('app', 'forms', "#{self.class.name.underscore}.yml")
16
11
 
17
- return [] unless File.exist? yaml_config_file_path
12
+ return @form_properties unless File.exist? yaml_config_file_path
18
13
 
19
- @form_properties = YAML.load_file(yaml_config_file_path).deep_symbolize_keys
20
- @form_properties.deep_merge! @form_properties_additional if @form_properties_additional
21
- @form_properties
14
+ @form_properties.deep_merge YAML.load_file(yaml_config_file_path).deep_symbolize_keys
22
15
  end
23
16
  end
@@ -24,7 +24,7 @@ module Tramway::Core::ExtendableFormsHelpers::ClassBuilder
24
24
  field = property[1][:object]
25
25
  define_file_property_assignment_method property, field
26
26
  else
27
- #next unless property[1][:validates].present?
27
+ # next unless property[1][:validates].present?
28
28
 
29
29
  define_assignment_method property
30
30
  end
@@ -5,14 +5,14 @@ module Tramway::Core::ExtendableFormsHelpers::Submit::ClassHelpers
5
5
  define_method 'submit' do |params|
6
6
  model.values ||= {}
7
7
  extended_params = extended(simple_properties, more_properties, params)
8
- params.each do |key, value|
9
- method_name = "#{key}="
10
- send method_name, value if respond_to?(method_name)
11
- end
8
+ every_attribute_set params
12
9
  model.values = extended_params.reduce(model.values) do |hash, pair|
13
10
  hash.merge! pair[0] => pair[1]
14
11
  end
15
- super(params) && model.errors.empty?
12
+
13
+ return unless model.errors.empty?
14
+
15
+ save_in_submit params
16
16
  end
17
17
  end
18
18
  end
@@ -4,4 +4,18 @@ module Tramway::Core::ExtendableFormsHelpers::Submit::ObjectHelpers
4
4
  def extended(simple_properties, more_properties, params)
5
5
  params.except(*simple_properties.keys).except(*jsonb_ignored_properties(more_properties)).permit!.to_h
6
6
  end
7
+
8
+ def every_attribute_set(params)
9
+ params.each do |key, value|
10
+ method_name = "#{key}="
11
+ send(method_name, value) if respond_to?(method_name)
12
+ end
13
+ end
14
+
15
+ def save_in_submit(_params)
16
+ result = save
17
+ result.tap do
18
+ collecting_associations_errors unless result
19
+ end
20
+ end
7
21
  end
@@ -12,7 +12,7 @@ class Tramway::Core::ApplicationRecord < ActiveRecord::Base
12
12
  state :removed
13
13
 
14
14
  event :remove do
15
- transition active: :remove
15
+ transition active: :removed
16
16
  end
17
17
  end
18
18
 
@@ -46,11 +46,16 @@ class Tramway::Core::ApplicationRecord < ActiveRecord::Base
46
46
  def uploader(attribute_name, uploader_name, **options)
47
47
  mount_uploader attribute_name, "#{uploader_name.to_s.camelize}Uploader".constantize
48
48
  @versions = options[:versions] if uploader_name == :photo
49
+ @extensions = options[:extensions]
49
50
  end
50
51
 
51
52
  def photo_versions
52
53
  @versions
53
54
  end
55
+
56
+ def file_extensions
57
+ @extensions
58
+ end
54
59
  end
55
60
 
56
61
  # FIXME: detect inhertited locales
@@ -14,6 +14,7 @@ class ApplicationUploader < CarrierWave::Uploader::Base
14
14
  def id_directory
15
15
  if model.respond_to?(:uuid)
16
16
  model.reload unless model.uuid.present?
17
+ model.uuid
17
18
  else
18
19
  model.id
19
20
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class FileUploader < ApplicationUploader
4
- def extension_white_list
5
- %w[pdf doc docx xls csv xlsx jpg svg png jpeg gif]
4
+ def extension_whitelist
5
+ model.class.file_extensions || %w[pdf doc docx xls csv xlsx jpg svg png jpeg gif]
6
6
  end
7
7
  end
@@ -8,7 +8,7 @@ module ImageDefaults
8
8
  [mounted_as, version_name].compact.join('_') << '.gif'
9
9
  end
10
10
 
11
- def extension_white_list
12
- %w[jpg jpeg gif png]
11
+ def extension_whitelist
12
+ model.class.file_extensions || %w[jpg jpeg gif png]
13
13
  end
14
14
  end
@@ -16,10 +16,6 @@ class PhotoUploader < ApplicationUploader
16
16
  end
17
17
  end
18
18
 
19
- def present?
20
- super && width.present? && height.present?
21
- end
22
-
23
19
  version :medium, if: :medium_version_is_needed? do
24
20
  process resize_to_fill: [400, 400]
25
21
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Tramway
4
4
  module Core
5
- VERSION = '1.18.0.1'
5
+ VERSION = '1.18.1.1'
6
6
  end
7
7
  end
@@ -21,6 +21,7 @@ class Tramway::Error < RuntimeError
21
21
  def raise_error(*coordinates, **options)
22
22
  @errors ||= YAML.load_file("#{Tramway::Core.root}/yaml/errors.yml").with_indifferent_access
23
23
  error = @errors.dig(*coordinates)
24
+ raise 'Error is not defined in YAML' unless error
24
25
  options.each do |pair|
25
26
  error.gsub!("%{#{pair[0]}}", pair[1].to_s)
26
27
  end
@@ -29,6 +29,7 @@ tramway:
29
29
  associations:
30
30
  object_helper:
31
31
  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}'`"
32
+ habtm_add_class_not_defined: "You should define class `%{class}` to be able add and remove `%{association_name}`"
32
33
  class_helper:
33
34
  model_does_not_have_association: "Model %{object_class} does not have association named `%{association_name}`"
34
35
  application_decorator:
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.18.0.1
4
+ version: 1.18.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Kalashnikov
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-04-24 00:00:00.000000000 Z
12
+ date: 2020-04-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: audited