tramway 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (177) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +1 -1
  3. data/README.md +12 -893
  4. data/Rakefile +8 -8
  5. data/app/components/tailwind_component.html.haml +0 -0
  6. data/app/components/tailwind_component.rb +6 -0
  7. data/app/components/tailwinds/navbar/button_component.html.haml +1 -0
  8. data/app/components/tailwinds/navbar/button_component.rb +14 -0
  9. data/config/routes.rb +0 -9
  10. data/lib/tasks/tramway_tasks.rake +5 -0
  11. data/lib/tramway/engine.rb +5 -10
  12. data/lib/tramway/version.rb +1 -1
  13. data/lib/tramway.rb +5 -140
  14. metadata +16 -545
  15. data/app/assets/config/tramway_core_manifest.js +0 -2
  16. data/app/assets/images/tramway/mona_lisa_from_prado.jpg +0 -0
  17. data/app/assets/images/tramway/mona_lisa_from_prado_square.jpg +0 -0
  18. data/app/assets/javascripts/bootstrap-datepicker-1.8.0.js +0 -2035
  19. data/app/assets/javascripts/bootstrap-datepicker-1.8.0.ru.min.js +0 -64
  20. data/app/assets/javascripts/ckeditor/config.js +0 -30
  21. data/app/assets/javascripts/ckeditor/plugins/image/dialogs/image.js +0 -1259
  22. data/app/assets/javascripts/ckeditor/plugins/image/icons/hidpi/image.png +0 -0
  23. data/app/assets/javascripts/ckeditor/plugins/image/icons/image.png +0 -0
  24. data/app/assets/javascripts/ckeditor/plugins/image/images/noimage.png +0 -0
  25. data/app/assets/javascripts/ckeditor/plugins/image/lang/en.js +0 -25
  26. data/app/assets/javascripts/ckeditor/plugins/image/lang/ru.js +0 -25
  27. data/app/assets/javascripts/ckeditor/plugins/image/plugin.js +0 -184
  28. data/app/assets/javascripts/ckeditor/plugins/youtube/images/icon-hdpi.png +0 -0
  29. data/app/assets/javascripts/ckeditor/plugins/youtube/images/icon.png +0 -0
  30. data/app/assets/javascripts/ckeditor/plugins/youtube/lang/en.js +0 -25
  31. data/app/assets/javascripts/ckeditor/plugins/youtube/lang/ru.js +0 -25
  32. data/app/assets/javascripts/ckeditor/plugins/youtube/plugin.js +0 -449
  33. data/app/assets/javascripts/tramway/application.js +0 -61
  34. data/app/assets/stylesheets/tramway/application.sass +0 -59
  35. data/app/controllers/tramway/application_controller.rb +0 -142
  36. data/app/controllers/tramway/concerns/auth_management.rb +0 -26
  37. data/app/controllers/tramway/concerns/filtering.rb +0 -43
  38. data/app/controllers/tramway/export/application_controller.rb +0 -5
  39. data/app/controllers/tramway/has_and_belongs_to_many_records_controller.rb +0 -26
  40. data/app/controllers/tramway/records_controller.rb +0 -86
  41. data/app/controllers/tramway/sessions_controller.rb +0 -49
  42. data/app/controllers/tramway/singletons_controller.rb +0 -48
  43. data/app/controllers/tramway/welcome_controller.rb +0 -17
  44. data/app/decorators/tramway/application_decorated_collection.rb +0 -15
  45. data/app/decorators/tramway/application_decorator.rb +0 -124
  46. data/app/decorators/tramway/associations/class_helper.rb +0 -45
  47. data/app/decorators/tramway/associations/object_helper.rb +0 -58
  48. data/app/decorators/tramway/attributes/view_helper.rb +0 -30
  49. data/app/decorators/tramway/concerns/attributes_decorator_helper.rb +0 -97
  50. data/app/decorators/tramway/concerns/table_builder.rb +0 -33
  51. data/app/decorators/tramway/default/values_helper.rb +0 -23
  52. data/app/decorators/tramway/delegating/class_helper.rb +0 -9
  53. data/app/decorators/tramway/user_decorator.rb +0 -49
  54. data/app/forms/admin/tramway/user_form.rb +0 -24
  55. data/app/forms/tramway/application_form.rb +0 -124
  56. data/app/forms/tramway/application_forms/association_class_helpers.rb +0 -7
  57. data/app/forms/tramway/application_forms/association_object_helpers.rb +0 -36
  58. data/app/forms/tramway/application_forms/constant_class_actions.rb +0 -7
  59. data/app/forms/tramway/application_forms/constant_object_actions.rb +0 -20
  60. data/app/forms/tramway/application_forms/frontend.rb +0 -12
  61. data/app/forms/tramway/application_forms/object_helpers.rb +0 -15
  62. data/app/forms/tramway/application_forms/properties_object_helper.rb +0 -16
  63. data/app/forms/tramway/application_forms/submit_helper.rb +0 -26
  64. data/app/forms/tramway/extendable_form.rb +0 -15
  65. data/app/forms/tramway/extendable_forms_helpers/class_builder.rb +0 -34
  66. data/app/forms/tramway/extendable_forms_helpers/ignored_properties_helper.rb +0 -11
  67. data/app/forms/tramway/extendable_forms_helpers/more_properties_helper.rb +0 -31
  68. data/app/forms/tramway/extendable_forms_helpers/properties_helper.rb +0 -16
  69. data/app/forms/tramway/extendable_forms_helpers/submit/class_helpers.rb +0 -18
  70. data/app/forms/tramway/extendable_forms_helpers/submit/object_helpers.rb +0 -21
  71. data/app/forms/tramway/extendable_forms_helpers/validators.rb +0 -40
  72. data/app/forms/tramway/extended_application_form.rb +0 -23
  73. data/app/forms/tramway/form_creator.rb +0 -7
  74. data/app/forms/tramway/session_form.rb +0 -26
  75. data/app/helpers/tramway/actions_helper.rb +0 -45
  76. data/app/helpers/tramway/additional_buttons_builder.rb +0 -12
  77. data/app/helpers/tramway/application_helper.rb +0 -27
  78. data/app/helpers/tramway/cases_helper.rb +0 -13
  79. data/app/helpers/tramway/copy_to_clipboard_helper.rb +0 -11
  80. data/app/helpers/tramway/focus_generator_helper.rb +0 -10
  81. data/app/helpers/tramway/frontend_helper.rb +0 -26
  82. data/app/helpers/tramway/inputs/associations_helper.rb +0 -30
  83. data/app/helpers/tramway/inputs/polymorphic_associations_helper.rb +0 -24
  84. data/app/helpers/tramway/inputs_helper.rb +0 -96
  85. data/app/helpers/tramway/model_helper.rb +0 -7
  86. data/app/helpers/tramway/navbar_helper.rb +0 -11
  87. data/app/helpers/tramway/records_helper.rb +0 -120
  88. data/app/helpers/tramway/russian_cases_helper.rb +0 -26
  89. data/app/helpers/tramway/singleton_helper.rb +0 -12
  90. data/app/helpers/tramway/state_machine_buttons_helper.rb +0 -61
  91. data/app/helpers/tramway/title_helper.rb +0 -25
  92. data/app/inputs/date_picker_input.rb +0 -4
  93. data/app/inputs/multiple_file_input.rb +0 -7
  94. data/app/models/tramway/application_record.rb +0 -62
  95. data/app/models/tramway/user.rb +0 -18
  96. data/app/uploaders/application_uploader.rb +0 -22
  97. data/app/uploaders/file_uploader.rb +0 -4
  98. data/app/uploaders/ico_uploader.rb +0 -7
  99. data/app/uploaders/image_defaults.rb +0 -14
  100. data/app/uploaders/photo_uploader.rb +0 -54
  101. data/app/views/layouts/tramway/application.html.haml +0 -32
  102. data/app/views/layouts/tramway/shared/_navbar.html.haml +0 -46
  103. data/app/views/tramway/404.haml +0 -1
  104. data/app/views/tramway/records/_form.html.haml +0 -22
  105. data/app/views/tramway/records/_list.html.haml +0 -38
  106. data/app/views/tramway/records/_search.html.haml +0 -34
  107. data/app/views/tramway/records/edit.html.haml +0 -1
  108. data/app/views/tramway/records/index.html.haml +0 -31
  109. data/app/views/tramway/records/new.html.haml +0 -1
  110. data/app/views/tramway/records/show.html.haml +0 -1
  111. data/app/views/tramway/sessions/new.html.haml +0 -9
  112. data/app/views/tramway/shared/_input.html.haml +0 -34
  113. data/app/views/tramway/shared/_input_extended.html.haml +0 -14
  114. data/app/views/tramway/shared/_messages.html.haml +0 -10
  115. data/app/views/tramway/shared/_show.html.haml +0 -35
  116. data/app/views/tramway/shared/errors/server_error.html.haml +0 -12
  117. data/app/views/tramway/shared/input_extended_types/_checkbox.html.haml +0 -1
  118. data/app/views/tramway/shared/input_extended_types/_select.html.haml +0 -16
  119. data/app/views/tramway/shared/input_extended_types/_simple.html.haml +0 -4
  120. data/app/views/tramway/shared/input_extended_types/_yes_no.html.haml +0 -4
  121. data/app/views/tramway/shared/show/_attribute_tr.html.haml +0 -9
  122. data/app/views/tramway/shared/show/associations/_habtm_row.html.haml +0 -14
  123. data/app/views/tramway/shared/show/associations/_list.html.haml +0 -21
  124. data/app/views/tramway/shared/show/associations/_row.html.haml +0 -19
  125. data/app/views/tramway/shared/show/associations/_table_row.html.haml +0 -21
  126. data/app/views/tramway/singletons/_form.html.haml +0 -15
  127. data/app/views/tramway/singletons/edit.html.haml +0 -1
  128. data/app/views/tramway/singletons/new.html.haml +0 -1
  129. data/app/views/tramway/singletons/show.html.haml +0 -1
  130. data/app/views/tramway/welcome/index.html.haml +0 -4
  131. data/config/initializers/assets.rb +0 -7
  132. data/config/initializers/ckeditor.rb +0 -9
  133. data/config/initializers/plurals.rb +0 -25
  134. data/config/locales/en/collections.yml +0 -4
  135. data/config/locales/en/date.yml +0 -16
  136. data/config/locales/en/dates.yml +0 -10
  137. data/config/locales/en/helpers.yml +0 -26
  138. data/config/locales/en/locale.yml +0 -11
  139. data/config/locales/en/messages.yml +0 -7
  140. data/config/locales/en/models.yml +0 -9
  141. data/config/locales/en/simple_form_extension.yml +0 -8
  142. data/config/locales/en/state_machines.yml +0 -8
  143. data/config/locales/ru/collections.yml +0 -4
  144. data/config/locales/ru/date.yml +0 -16
  145. data/config/locales/ru/dates.yml +0 -10
  146. data/config/locales/ru/helpers.yml +0 -30
  147. data/config/locales/ru/locale.yml +0 -6
  148. data/config/locales/ru/messages.yml +0 -7
  149. data/config/locales/ru/models.yml +0 -21
  150. data/config/locales/ru/simple_form_extension.yml +0 -8
  151. data/config/locales/ru/state_machines.yml +0 -8
  152. data/lib/string.rb +0 -18
  153. data/lib/tramway/application.rb +0 -10
  154. data/lib/tramway/class_name_helpers.rb +0 -15
  155. data/lib/tramway/collection.rb +0 -9
  156. data/lib/tramway/collections/helper.rb +0 -21
  157. data/lib/tramway/collections.rb +0 -4
  158. data/lib/tramway/error.rb +0 -32
  159. data/lib/tramway/forms.rb +0 -5
  160. data/lib/tramway/generators/install_generator.rb +0 -49
  161. data/lib/tramway/generators/model_generator.rb +0 -105
  162. data/lib/tramway/generators/templates/create_tramway_users.rb +0 -18
  163. data/lib/tramway/generators/templates/decorator.rb.erb +0 -56
  164. data/lib/tramway/generators/templates/form.rb.erb +0 -22
  165. data/lib/tramway/generators/templates/initializers/simple_form.rb +0 -23
  166. data/lib/tramway/generators/templates/initializers/simple_form_bootstrap.rb +0 -133
  167. data/lib/tramway/generators.rb +0 -6
  168. data/lib/tramway/navbar.rb +0 -44
  169. data/lib/tramway/notifications.rb +0 -12
  170. data/lib/tramway/record_routes_helper.rb +0 -23
  171. data/lib/tramway/records_models.rb +0 -60
  172. data/lib/tramway/singleton_models.rb +0 -32
  173. data/lib/tramway/spec/helpers/navbar_helper.rb +0 -7
  174. data/lib/tramway/spec/helpers/tramway_helpers.rb +0 -47
  175. data/lib/tramway/welcome_page_actions.rb +0 -5
  176. data/lib/tramway/yaml/errors.yml +0 -50
  177. data/lib/validators/presence_validator.rb +0 -9
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Tramway::Concerns::TableBuilder
4
- def table(**options, &block)
5
- content_tag(:table, class: 'table table-bordered table-striped', **options) do
6
- yield if block
7
- end
8
- end
9
-
10
- def thead(**options, &block)
11
- content_tag(:thead, **options) do
12
- yield if block
13
- end
14
- end
15
-
16
- def th(**options, &block)
17
- content_tag(:th, **options) do
18
- yield if block
19
- end
20
- end
21
-
22
- def td(**options, &block)
23
- content_tag(:td, **options) do
24
- yield if block
25
- end
26
- end
27
-
28
- def tr(**options, &block)
29
- content_tag(:tr, **options) do
30
- yield if block
31
- end
32
- end
33
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Tramway::Default::ValuesHelper
4
- def collections
5
- [:all]
6
- end
7
-
8
- def list_attributes
9
- []
10
- end
11
-
12
- def show_attributes
13
- []
14
- end
15
-
16
- def show_associations
17
- []
18
- end
19
-
20
- def list_filters
21
- {}
22
- end
23
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Tramway::Delegating::ClassHelper
4
- def delegate_attributes(*attributes)
5
- attributes.each do |attr|
6
- delegate attr, to: :object
7
- end
8
- end
9
- end
@@ -1,49 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Tramway::UserDecorator < Tramway::ApplicationDecorator
4
- class << self
5
- def collections
6
- [:all]
7
- end
8
-
9
- def list_attributes
10
- [:email]
11
- end
12
-
13
- def show_attributes
14
- %i[email first_name last_name phone role created_at updated_at credential_text]
15
- end
16
-
17
- if defined? Tramway::Conference
18
- def show_associations
19
- [:social_networks]
20
- end
21
- end
22
- end
23
-
24
- decorate_association :social_networks, as: :record if defined? Tramway::Conference
25
-
26
- delegate_attributes :first_name, :last_name, :email, :phone, :role, :created_at, :updated_at, :admin?
27
-
28
- def name
29
- "#{object&.first_name} #{object&.last_name}"
30
- end
31
-
32
- def credential_text
33
- content_tag(:pre) do
34
- id = "credential_text_#{object.id}"
35
-
36
- concat(content_tag(:span, id:) do
37
- text = "URL: #{ENV['PROJECT_URL']}\n"
38
- text += "Email: #{object.email}\n"
39
- "#{text}Password: "
40
- end)
41
-
42
- concat(content_tag(:br))
43
-
44
- concat copy_to_clipboard id
45
- end
46
- end
47
-
48
- alias title name
49
- end
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Admin::Tramway::UserForm < ::Tramway::ApplicationForm
4
- self.model_class = Tramway::User
5
-
6
- properties :email, :password, :first_name, :last_name, :role, :phone
7
-
8
- # fix me
9
- unless model_class.columns_hash['project_id'].present?
10
- validates :email, email: true, uniqueness: true,
11
- on: :destroy
12
- end
13
-
14
- def initialize(object)
15
- super(object).tap do
16
- form_properties email: :string,
17
- password: :string,
18
- first_name: :string,
19
- last_name: :string,
20
- phone: :string,
21
- role: :default
22
- end
23
- end
24
- end
@@ -1,124 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Tramway::ApplicationForm
4
- include Tramway::ApplicationForms::AssociationObjectHelpers
5
- include Tramway::ApplicationForms::ConstantObjectActions
6
- include Tramway::ApplicationForms::PropertiesObjectHelper
7
- include Tramway::ApplicationForms::ObjectHelpers
8
- include Tramway::ApplicationForms::SubmitHelper
9
-
10
- attr_accessor :submit_message
11
-
12
- def initialize(object = nil)
13
- tap do
14
- @object = object
15
- @@model_class = object.class
16
- @@enumerized_attributes = object.class.try :enumerized_attributes
17
- @@associations ||= []
18
-
19
- self.class.full_class_name_associations&.each do |association, class_name|
20
- define_association_method association, class_name
21
- end
22
-
23
- delegating object
24
- end
25
- end
26
-
27
- def model_name
28
- @@model_class.model_name
29
- end
30
-
31
- def associations
32
- @@associations
33
- end
34
-
35
- class << self
36
- include Tramway::ApplicationForms::AssociationClassHelpers
37
- include Tramway::ApplicationForms::ConstantClassActions
38
- include Tramway::ApplicationForms::Frontend
39
-
40
- delegate :defined_enums, to: :model_class
41
-
42
- def properties(*props)
43
- props.each { |prop| property prop }
44
- end
45
-
46
- def property(prop)
47
- @@properties ||= []
48
- @@properties << prop
49
- delegate prop, to: :model
50
- define_method("#{prop}=") do |value|
51
- model.send "#{prop}=", value
52
- end
53
- end
54
-
55
- def association(property)
56
- properties property
57
- @@associations = ((defined?(@@associations) && @@associations) || []) + [property]
58
- end
59
-
60
- def full_class_name_associations
61
- @@associations&.reduce({}) do |hash, association|
62
- options = @@model_class.reflect_on_all_associations(:belongs_to).select do |a|
63
- a.name == association.to_sym
64
- end.first&.options
65
- add_polymorphic_association hash, association, options
66
- end
67
- end
68
-
69
- def add_polymorphic_association(hash, association, options)
70
- if options&.dig(:polymorphic)
71
- hash.merge association => @@model_class.send("#{association}_type").values
72
- elsif options
73
- hash.merge(association => (options[:class_name] || association.to_s.camelize).constantize)
74
- else
75
- hash
76
- end
77
- end
78
-
79
- def full_class_name_association(association_name)
80
- full_class_name_associations[association_name]
81
- end
82
-
83
- def reflect_on_association(*args)
84
- @@model_class.reflect_on_association(*args)
85
- end
86
-
87
- def enumerized_attributes
88
- @@enumerized_attributes
89
- end
90
-
91
- def model_class
92
- if defined?(@@model_class) && @@model_class
93
- @@model_class
94
- else
95
- model_class_name ||= name.to_s.sub(/Form$/, '')
96
- begin
97
- @@model_class = model_class_name.constantize
98
- rescue StandardError
99
- Tramway::Error.raise_error :tramway, :application_form, :model_class, :there_is_not_model_class,
100
- name:, model_class_name:
101
- end
102
- end
103
- end
104
-
105
- def model_class=(name)
106
- @@model_class = name
107
- end
108
-
109
- def validates(attribute, **options)
110
- if !defined?(@@model_class) || @@model_class.nil?
111
- Tramway::Error.raise_error(:tramway, :application_form, :validates, :you_need_to_set_model_class)
112
- end
113
- @@model_class.validates attribute, **options
114
- end
115
- end
116
-
117
- private
118
-
119
- def collecting_associations_errors
120
- @@associations.each do |association|
121
- model.send("#{association}=", send(association)) if errors.details[association] == [{ error: :blank }]
122
- end
123
- end
124
- end
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Tramway::ApplicationForms::AssociationClassHelpers
4
- def associations(*properties)
5
- properties.each { |property| association property }
6
- end
7
- end
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Tramway::ApplicationForms::AssociationObjectHelpers
4
- def define_association_method(association, class_name)
5
- if class_name.is_a? Array
6
- define_polymorphic_association association
7
- else
8
- self.class.send(:define_method, "#{association}=") do |value|
9
- model.send "#{association}_id=", value
10
- model.send "#{association}=", class_name.find(value)
11
- end
12
- end
13
- end
14
-
15
- def define_polymorphic_association(association)
16
- self.class.send(:define_method, "#{association}=") do |value|
17
- if value.present?
18
- if association_class(value).nil?
19
- Tramway::Error.raise_error :tramway, :application_form, :initialize, :polymorphic_class_is_nil,
20
- association_name: association
21
- else
22
- model.send "#{association}=", association_class(value).find(value.split('_')[-1])
23
- send "#{association}_type=", association_class(value).to_s
24
- end
25
- end
26
- end
27
- end
28
-
29
- private
30
-
31
- def association_class(value)
32
- association_class_object = value.split('_')[0..-2].join('_').camelize
33
- association_class_object = association_class_object.constantize if association_class_object.is_a? String
34
- association_class_object
35
- end
36
- end
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Tramway::ApplicationForms::ConstantClassActions
4
- def validation_group_class
5
- ActiveModel
6
- end
7
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Tramway::ApplicationForms::ConstantObjectActions
4
- def delegating(object)
5
- %i[to_key errors].each { |method| self.class.send(:define_method, method) { object.send method } }
6
- end
7
-
8
- def build_errors; end
9
-
10
- def attributes
11
- properties.reduce({}) do |hash, property|
12
- value = if model.respond_to? :values
13
- model.values[property.first.to_s]
14
- else
15
- model.send(property.first.to_s)
16
- end
17
- hash.merge! property.first => value
18
- end
19
- end
20
- end
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Tramway::ApplicationForms::Frontend
4
- def react_component(on: false)
5
- @react_component = on
6
- end
7
-
8
- def react_component?
9
- @react_component ||= false
10
- @react_component
11
- end
12
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Tramway::ApplicationForms::ObjectHelpers
4
- def to_model
5
- self
6
- end
7
-
8
- def persisted?
9
- model.id.nil?
10
- end
11
-
12
- def model
13
- @object
14
- end
15
- end
@@ -1,16 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Tramway::ApplicationForms::PropertiesObjectHelper
4
- def form_properties(**args)
5
- @form_properties = args
6
- end
7
-
8
- def properties
9
- @form_properties ||= {}
10
- yaml_config_file_path = Rails.root.join('app', 'forms', "#{self.class.name.underscore}.yml")
11
-
12
- return @form_properties unless File.exist? yaml_config_file_path
13
-
14
- @form_properties.deep_merge YAML.load_file(yaml_config_file_path).deep_symbolize_keys
15
- end
16
- end
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Tramway::ApplicationForms::SubmitHelper
4
- def submit(params)
5
- if params
6
- params.each { |key, value| send("#{key}=", value) }
7
- result = save
8
- result.tap do
9
- collecting_associations_errors unless result
10
- end
11
- else
12
- Tramway::Error.raise_error(:application_form, :submit, :params_should_not_be_nil)
13
- end
14
- end
15
-
16
- def save
17
- model.save
18
- rescue ArgumentError => e
19
- Tramway::Error.raise_error :tramway, :application_form, :save, :argument_error, message: e.message
20
- rescue StandardError => e
21
- raise e unless e.try :name
22
-
23
- Tramway::Error.raise_error :tramway, :application_form, :save, :looks_like_you_have_method,
24
- method_name: e.name.to_s.gsub('=', ''), model_class: model.class, class_name: self.class
25
- end
26
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Tramway::ExtendableForm
4
- class << self
5
- include Tramway::ExtendableFormsHelpers::ClassBuilder
6
-
7
- def new(name, simple_properties: {}, **more_properties)
8
- if Object.const_defined? name
9
- name.constantize
10
- else
11
- build_form_class name, simple_properties, more_properties
12
- end
13
- end
14
- end
15
- end
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Tramway::ExtendableFormsHelpers::ClassBuilder
4
- def build_form_class(name, simple_properties, more_properties)
5
- Object.const_set(name, Class.new(::Tramway::ApplicationForm) do
6
- properties(*simple_properties.keys) if simple_properties.keys.any?
7
-
8
- include Tramway::ExtendableFormsHelpers::Submit::ObjectHelpers
9
- include Tramway::ExtendableFormsHelpers::Validators
10
- extend Tramway::ExtendableFormsHelpers::Submit::ClassHelpers
11
- extend Tramway::ExtendableFormsHelpers::PropertiesHelper
12
- extend Tramway::ExtendableFormsHelpers::MorePropertiesHelper
13
- extend Tramway::ExtendableFormsHelpers::IgnoredPropertiesHelper
14
-
15
- define_submit_method simple_properties, more_properties
16
- define_properties_method simple_properties, more_properties
17
- define_ignored_properties_method
18
-
19
- more_properties.each do |property|
20
- define_property_method property[0]
21
-
22
- case property[1][:object].field_type
23
- when 'file'
24
- field = property[1][:object]
25
- define_file_property_assignment_method property, field
26
- else
27
- # next unless property[1][:validates].present?
28
-
29
- define_assignment_method property
30
- end
31
- end
32
- end)
33
- end
34
- end
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Tramway::ExtendableFormsHelpers::IgnoredPropertiesHelper
4
- def define_ignored_properties_method
5
- define_method :jsonb_ignored_properties do |properties|
6
- properties.map do |property|
7
- property[0].to_s if property[1][:object].field_type == 'file'
8
- end.compact
9
- end
10
- end
11
- end
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Tramway::ExtendableFormsHelpers::MorePropertiesHelper
4
- def define_property_method(property_name)
5
- define_method property_name do
6
- model.values[property_name] if model.values
7
- end
8
- end
9
-
10
- def define_assignment_method(property)
11
- define_method "#{property[0]}=" do |value|
12
- if property[1][:validates].present?
13
- property[1][:validates].each do |pair|
14
- make_validates property[0], pair, value
15
- end
16
- else
17
- model.values.merge property[0] => value
18
- end
19
- end
20
- end
21
-
22
- def define_file_property_assignment_method(property, field)
23
- define_method "#{property[0]}=" do |value|
24
- file_instance = property[1][:association_model].find_or_create_by(
25
- "#{model.class.name.underscore}_id" => model.id, "#{field.class.name.underscore}_id" => field.id
26
- )
27
- file_instance.file = value
28
- file_instance.save!
29
- end
30
- end
31
- end
@@ -1,16 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Tramway::ExtendableFormsHelpers::PropertiesHelper
4
- def define_properties_method(simple_properties, more_properties)
5
- define_method 'properties' do
6
- hash = simple_properties.each_with_object({}) do |property, h|
7
- h.merge! property[0] => property[1] unless model.class.state_machines_names.include?(property[0])
8
- end
9
- more_properties.reduce(hash) do |h, property|
10
- h.merge! property[0] => {
11
- extended_form_property: property[1][:object]
12
- }
13
- end
14
- end
15
- end
16
- end
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Tramway::ExtendableFormsHelpers::Submit::ClassHelpers
4
- def define_submit_method(simple_properties, more_properties)
5
- define_method 'submit' do |params|
6
- model.values ||= {}
7
- extended_params = extended(simple_properties, more_properties, params)
8
- every_attribute_set params
9
- model.values = extended_params.reduce(model.values) do |hash, (key, value)|
10
- hash.merge! key => value
11
- end
12
-
13
- return unless model.errors.empty?
14
-
15
- save_in_submit params
16
- end
17
- end
18
- end
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Tramway::ExtendableFormsHelpers::Submit::ObjectHelpers
4
- def extended(simple_properties, more_properties, params)
5
- params.except(*simple_properties.keys).except(*jsonb_ignored_properties(more_properties)).permit!.to_h
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
21
- end
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Tramway::ExtendableFormsHelpers::Validators
4
- def make_validates(property_name, validation, value)
5
- case validation[0].to_s
6
- when 'presence'
7
- presence_validator property_name, validation, value
8
- when 'inclusion'
9
- inclusion_validator property_name, validation, value
10
- end
11
- end
12
-
13
- def presence_validator(property_name, validation, value)
14
- validator_object = PresenceValidator.new(attributes: :not_blank)
15
- return if validation[1] == 'false'
16
- return if validation[1] == 'true' && validator_object.send(:valid?, value)
17
-
18
- model.errors.add(
19
- property_name,
20
- I18n.t(
21
- "activerecord.errors.models.#{model.class.name.underscore}.attributes.default.#{validation[0]}",
22
- value:
23
- )
24
- )
25
- end
26
-
27
- def inclusion_validator(property_name, validation, value)
28
- in_array = validation[1][:in]
29
-
30
- return if value.in? in_array
31
-
32
- model.errors.add(
33
- property_name,
34
- I18n.t(
35
- "activerecord.errors.models.#{model.class.name.underscore}.attributes.default.#{validation[0]}",
36
- value:
37
- )
38
- )
39
- end
40
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Tramway::ExtendedApplicationForm < Tramway::ApplicationForm
4
- class << self
5
- def properties(*args)
6
- @@extendable_properties ||= []
7
- @@extendable_properties += args
8
- super(*args)
9
- end
10
- end
11
-
12
- def initialize(model)
13
- @@extendable_properties.each do |prop|
14
- next if model.respond_to? prop
15
-
16
- model.class.define_method prop do
17
- end
18
- model.class.define_method "#{prop}=" do |value|
19
- end
20
- end
21
- super
22
- end
23
- end
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Tramway::FormCreator
4
- def self.create_form_class
5
- raise 'Please, implement `create_form_class` for your class'
6
- end
7
- end
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Tramway::SessionForm < Tramway::ApplicationForm
4
- properties :email
5
- attr_accessor :password
6
-
7
- def initialize(object)
8
- object = Tramway::User.new unless object.present?
9
-
10
- super
11
- end
12
-
13
- def validate(params)
14
- (add_wrong_email_or_password_error && return) if model.new_record?
15
-
16
- (!model.new_record? && model.authenticate(params[:password])).tap do |result|
17
- add_wrong_email_or_password_error unless result
18
- end
19
- end
20
-
21
- private
22
-
23
- def add_wrong_email_or_password_error
24
- errors.add(:email, I18n.t('errors.wrong_email_or_password'))
25
- end
26
- end