tramway 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/MIT-LICENSE +20 -0
- data/README.md +889 -15
- data/Rakefile +8 -8
- data/app/assets/config/tramway_core_manifest.js +2 -0
- data/app/assets/images/tramway/mona_lisa_from_prado.jpg +0 -0
- data/app/assets/images/tramway/mona_lisa_from_prado_square.jpg +0 -0
- data/app/assets/javascripts/bootstrap-datepicker-1.8.0.js +2035 -0
- data/app/assets/javascripts/bootstrap-datepicker-1.8.0.ru.min.js +64 -0
- data/app/assets/javascripts/ckeditor/config.js +30 -0
- data/app/assets/javascripts/ckeditor/plugins/image/dialogs/image.js +1259 -0
- data/app/assets/javascripts/ckeditor/plugins/image/icons/hidpi/image.png +0 -0
- data/app/assets/javascripts/ckeditor/plugins/image/icons/image.png +0 -0
- data/app/assets/javascripts/ckeditor/plugins/image/images/noimage.png +0 -0
- data/app/assets/javascripts/ckeditor/plugins/image/lang/en.js +25 -0
- data/app/assets/javascripts/ckeditor/plugins/image/lang/ru.js +25 -0
- data/app/assets/javascripts/ckeditor/plugins/image/plugin.js +184 -0
- data/app/assets/javascripts/ckeditor/plugins/youtube/images/icon-hdpi.png +0 -0
- data/app/assets/javascripts/ckeditor/plugins/youtube/images/icon.png +0 -0
- data/app/assets/javascripts/ckeditor/plugins/youtube/lang/en.js +25 -0
- data/app/assets/javascripts/ckeditor/plugins/youtube/lang/ru.js +25 -0
- data/app/assets/javascripts/ckeditor/plugins/youtube/plugin.js +449 -0
- data/app/assets/javascripts/tramway/application.js +60 -0
- data/app/assets/stylesheets/tramway/application.sass +60 -0
- data/app/assets/stylesheets/tramway/bootstrap-datepicker-1.8.0.css +477 -0
- data/app/controllers/concerns/auth_management.rb +26 -0
- data/app/controllers/concerns/filtering.rb +43 -0
- data/app/controllers/tramway/application_controller.rb +142 -0
- data/app/controllers/tramway/export/application_controller.rb +5 -0
- data/app/controllers/tramway/has_and_belongs_to_many_records_controller.rb +26 -0
- data/app/controllers/tramway/records_controller.rb +86 -0
- data/app/controllers/tramway/sessions_controller.rb +49 -0
- data/app/controllers/tramway/singletons_controller.rb +48 -0
- data/app/controllers/tramway/welcome_controller.rb +17 -0
- data/app/decorators/tramway/application_decorated_collection.rb +15 -0
- data/app/decorators/tramway/application_decorator.rb +124 -0
- data/app/decorators/tramway/associations/class_helper.rb +45 -0
- data/app/decorators/tramway/associations/object_helper.rb +58 -0
- data/app/decorators/tramway/attributes/view_helper.rb +30 -0
- data/app/decorators/tramway/concerns/attributes_decorator_helper.rb +97 -0
- data/app/decorators/tramway/concerns/table_builder.rb +33 -0
- data/app/decorators/tramway/default/values_helper.rb +23 -0
- data/app/decorators/tramway/delegating/class_helper.rb +9 -0
- data/app/decorators/tramway/user_decorator.rb +49 -0
- data/app/forms/admin/tramway/user_form.rb +24 -0
- data/app/forms/tramway/application_form.rb +124 -0
- data/app/forms/tramway/application_forms/association_class_helpers.rb +7 -0
- data/app/forms/tramway/application_forms/association_object_helpers.rb +36 -0
- data/app/forms/tramway/application_forms/constant_class_actions.rb +7 -0
- data/app/forms/tramway/application_forms/constant_object_actions.rb +20 -0
- data/app/forms/tramway/application_forms/frontend.rb +12 -0
- data/app/forms/tramway/application_forms/object_helpers.rb +15 -0
- data/app/forms/tramway/application_forms/properties_object_helper.rb +16 -0
- data/app/forms/tramway/application_forms/submit_helper.rb +26 -0
- data/app/forms/tramway/extendable_form.rb +15 -0
- data/app/forms/tramway/extendable_forms_helpers/class_builder.rb +34 -0
- data/app/forms/tramway/extendable_forms_helpers/ignored_properties_helper.rb +11 -0
- data/app/forms/tramway/extendable_forms_helpers/more_properties_helper.rb +31 -0
- data/app/forms/tramway/extendable_forms_helpers/properties_helper.rb +16 -0
- data/app/forms/tramway/extendable_forms_helpers/submit/class_helpers.rb +18 -0
- data/app/forms/tramway/extendable_forms_helpers/submit/object_helpers.rb +21 -0
- data/app/forms/tramway/extendable_forms_helpers/validators.rb +40 -0
- data/app/forms/tramway/extended_application_form.rb +23 -0
- data/app/forms/tramway/form_creator.rb +7 -0
- data/app/forms/tramway/session_form.rb +26 -0
- data/app/helpers/tramway/actions_helper.rb +45 -0
- data/app/helpers/tramway/additional_buttons_builder.rb +12 -0
- data/app/helpers/tramway/application_helper.rb +27 -0
- data/app/helpers/tramway/cases_helper.rb +13 -0
- data/app/helpers/tramway/copy_to_clipboard_helper.rb +11 -0
- data/app/helpers/tramway/focus_generator_helper.rb +10 -0
- data/app/helpers/tramway/frontend_helper.rb +26 -0
- data/app/helpers/tramway/inputs/associations_helper.rb +30 -0
- data/app/helpers/tramway/inputs/polymorphic_associations_helper.rb +24 -0
- data/app/helpers/tramway/inputs_helper.rb +96 -0
- data/app/helpers/tramway/model_helper.rb +7 -0
- data/app/helpers/tramway/navbar_helper.rb +11 -0
- data/app/helpers/tramway/records_helper.rb +120 -0
- data/app/helpers/tramway/russian_cases_helper.rb +26 -0
- data/app/helpers/tramway/singleton_helper.rb +12 -0
- data/app/helpers/tramway/state_machine_buttons_helper.rb +61 -0
- data/app/helpers/tramway/title_helper.rb +25 -0
- data/app/inputs/date_picker_input.rb +4 -0
- data/app/inputs/multiple_file_input.rb +7 -0
- data/app/models/tramway/application_record.rb +60 -0
- data/app/models/tramway/user.rb +23 -0
- data/app/uploaders/application_uploader.rb +22 -0
- data/app/uploaders/file_uploader.rb +4 -0
- data/app/uploaders/ico_uploader.rb +7 -0
- data/app/uploaders/image_defaults.rb +14 -0
- data/app/uploaders/photo_uploader.rb +54 -0
- data/app/views/layouts/tramway/application.html.haml +32 -0
- data/app/views/layouts/tramway/shared/_navbar.html.haml +46 -0
- data/app/views/tramway/404.haml +1 -0
- data/app/views/tramway/records/_form.html.haml +22 -0
- data/app/views/tramway/records/_list.html.haml +38 -0
- data/app/views/tramway/records/_search.html.haml +34 -0
- data/app/views/tramway/records/edit.html.haml +1 -0
- data/app/views/tramway/records/index.html.haml +31 -0
- data/app/views/tramway/records/new.html.haml +1 -0
- data/app/views/tramway/records/show.html.haml +1 -0
- data/app/views/tramway/sessions/new.html.haml +9 -0
- data/app/views/tramway/shared/_input.html.haml +34 -0
- data/app/views/tramway/shared/_input_extended.html.haml +14 -0
- data/app/views/tramway/shared/_messages.html.haml +10 -0
- data/app/views/tramway/shared/_show.html.haml +35 -0
- data/app/views/tramway/shared/errors/server_error.html.haml +12 -0
- data/app/views/tramway/shared/input_extended_types/_checkbox.html.haml +1 -0
- data/app/views/tramway/shared/input_extended_types/_select.html.haml +16 -0
- data/app/views/tramway/shared/input_extended_types/_simple.html.haml +4 -0
- data/app/views/tramway/shared/input_extended_types/_yes_no.html.haml +4 -0
- data/app/views/tramway/shared/show/_attribute_tr.html.haml +9 -0
- data/app/views/tramway/shared/show/associations/_habtm_row.html.haml +14 -0
- data/app/views/tramway/shared/show/associations/_list.html.haml +21 -0
- data/app/views/tramway/shared/show/associations/_row.html.haml +19 -0
- data/app/views/tramway/shared/show/associations/_table_row.html.haml +21 -0
- data/app/views/tramway/singletons/_form.html.haml +15 -0
- data/app/views/tramway/singletons/edit.html.haml +1 -0
- data/app/views/tramway/singletons/new.html.haml +1 -0
- data/app/views/tramway/singletons/show.html.haml +1 -0
- data/app/views/tramway/welcome/index.html.haml +4 -0
- data/config/initializers/assets.rb +7 -0
- data/config/initializers/carrierwave.rb +5 -0
- data/config/initializers/ckeditor.rb +9 -0
- data/config/initializers/plurals.rb +25 -0
- data/config/locales/en/collections.yml +4 -0
- data/config/locales/en/date.yml +16 -0
- data/config/locales/en/dates.yml +10 -0
- data/config/locales/en/helpers.yml +26 -0
- data/config/locales/en/locale.yml +11 -0
- data/config/locales/en/messages.yml +7 -0
- data/config/locales/en/models.yml +9 -0
- data/config/locales/en/simple_form_extension.yml +8 -0
- data/config/locales/en/state_machines.yml +8 -0
- data/config/locales/ru/collections.yml +4 -0
- data/config/locales/ru/date.yml +16 -0
- data/config/locales/ru/dates.yml +10 -0
- data/config/locales/ru/helpers.yml +30 -0
- data/config/locales/ru/locale.yml +6 -0
- data/config/locales/ru/messages.yml +7 -0
- data/config/locales/ru/models.yml +21 -0
- data/config/locales/ru/simple_form_extension.yml +8 -0
- data/config/locales/ru/state_machines.yml +8 -0
- data/config/routes.rb +13 -0
- data/lib/string.rb +18 -0
- data/lib/tramway/application.rb +10 -0
- data/lib/tramway/class_name_helpers.rb +15 -0
- data/lib/tramway/collection.rb +9 -0
- data/lib/tramway/collections/helper.rb +21 -0
- data/lib/tramway/collections.rb +4 -0
- data/lib/tramway/engine.rb +14 -0
- data/lib/tramway/error.rb +32 -0
- data/lib/tramway/forms.rb +5 -0
- data/lib/tramway/generators/install_generator.rb +49 -0
- data/lib/tramway/generators/model_generator.rb +105 -0
- data/lib/tramway/generators/templates/create_tramway_users.rb +18 -0
- data/lib/tramway/generators/templates/decorator.rb.erb +56 -0
- data/lib/tramway/generators/templates/form.rb.erb +22 -0
- data/lib/tramway/generators/templates/initializers/simple_form.rb +23 -0
- data/lib/tramway/generators/templates/initializers/simple_form_bootstrap.rb +133 -0
- data/lib/tramway/generators.rb +4 -0
- data/lib/tramway/navbar.rb +44 -0
- data/lib/tramway/notifications.rb +12 -0
- data/lib/tramway/record_routes_helper.rb +23 -0
- data/lib/tramway/records_models.rb +52 -0
- data/lib/tramway/singleton_models.rb +32 -0
- data/lib/tramway/spec/helpers/navbar_helper.rb +7 -0
- data/lib/tramway/spec/helpers/tramway_helpers.rb +47 -0
- data/lib/tramway/version.rb +3 -1
- data/lib/tramway/welcome_page_actions.rb +5 -0
- data/lib/tramway/yaml/errors.yml +49 -0
- data/lib/tramway.rb +143 -2
- data/lib/validators/presence_validator.rb +9 -0
- metadata +582 -25
- data/.gitignore +0 -9
- data/.travis.yml +0 -5
- data/Gemfile +0 -6
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/tramway.gemspec +0 -26
@@ -0,0 +1,16 @@
|
|
1
|
+
en:
|
2
|
+
defaults: &defaults
|
3
|
+
month_names:
|
4
|
+
[~, January, February, March, April, May, June, July, August, September, October, November, December]
|
5
|
+
formats:
|
6
|
+
default: "%B %d %Y"
|
7
|
+
short: "%m/%d/%Y"
|
8
|
+
date:
|
9
|
+
<<: *defaults
|
10
|
+
order:
|
11
|
+
- :day
|
12
|
+
- :month
|
13
|
+
- :year
|
14
|
+
time:
|
15
|
+
<<: *defaults
|
16
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
en:
|
2
|
+
helpers:
|
3
|
+
links:
|
4
|
+
enter: Sign In
|
5
|
+
save: Save
|
6
|
+
back: Back
|
7
|
+
sign_out: Sign Out
|
8
|
+
actions: Actions
|
9
|
+
open: Open
|
10
|
+
actions:
|
11
|
+
create: Create
|
12
|
+
update: Update
|
13
|
+
search: Search
|
14
|
+
reset: Reset
|
15
|
+
download: Download
|
16
|
+
add: Add
|
17
|
+
scope:
|
18
|
+
found: Found
|
19
|
+
"yes": yes
|
20
|
+
"no": no
|
21
|
+
tramway:
|
22
|
+
auth:
|
23
|
+
web:
|
24
|
+
sessions:
|
25
|
+
new:
|
26
|
+
title: Sign In
|
@@ -0,0 +1,16 @@
|
|
1
|
+
ru:
|
2
|
+
defaults: &defaults
|
3
|
+
month_names:
|
4
|
+
[~, Январь, Февраль, Март, Апрель, Май, Июнь, Июль, Август, Сентябрь, Октябрь, Ноябрь, Декабрь]
|
5
|
+
formats:
|
6
|
+
default: "%d. %B %Y"
|
7
|
+
short: "%d.%m.%Y"
|
8
|
+
date:
|
9
|
+
<<: *defaults
|
10
|
+
order:
|
11
|
+
- :day
|
12
|
+
- :month
|
13
|
+
- :year
|
14
|
+
time:
|
15
|
+
<<: *defaults
|
16
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
ru:
|
2
|
+
helpers:
|
3
|
+
links:
|
4
|
+
enter: Войти
|
5
|
+
save: Сохранить
|
6
|
+
back: Назад
|
7
|
+
sign_out: Выйти
|
8
|
+
actions: Действия
|
9
|
+
open: Открыть
|
10
|
+
actions:
|
11
|
+
create: Создать
|
12
|
+
update: Редактировать
|
13
|
+
search: Искать
|
14
|
+
reset: Сбросить
|
15
|
+
download: Скачать
|
16
|
+
add: Добавить
|
17
|
+
scope:
|
18
|
+
found: Найденные
|
19
|
+
"yes": Да
|
20
|
+
"no": Нет
|
21
|
+
tramway:
|
22
|
+
admin:
|
23
|
+
welcome:
|
24
|
+
index:
|
25
|
+
title: Админка
|
26
|
+
auth:
|
27
|
+
web:
|
28
|
+
sessions:
|
29
|
+
new:
|
30
|
+
title: Авторизация
|
@@ -0,0 +1,21 @@
|
|
1
|
+
ru:
|
2
|
+
activerecord:
|
3
|
+
models:
|
4
|
+
tramway/user/user: Пользователь
|
5
|
+
attributes:
|
6
|
+
tramway/application_record:
|
7
|
+
created_at: Создан
|
8
|
+
updated_at: Обновлён
|
9
|
+
state: Статус
|
10
|
+
tramway/user:
|
11
|
+
password: Пароль
|
12
|
+
errors:
|
13
|
+
models:
|
14
|
+
tramway/user/user:
|
15
|
+
attributes:
|
16
|
+
email:
|
17
|
+
email: имеет неверный формат
|
18
|
+
cases:
|
19
|
+
tramway/user:
|
20
|
+
plural: пользователи
|
21
|
+
genitive: пользователя
|
data/config/routes.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
Tramway::Engine.routes.draw do
|
4
|
+
mount Tramway::Export::Engine, at: '/' if defined? Tramway::Export::Engine
|
5
|
+
|
6
|
+
root to: 'welcome#index'
|
7
|
+
|
8
|
+
resources :records
|
9
|
+
resource :singleton, only: %i[new create show edit update]
|
10
|
+
resources :has_and_belongs_to_many_records, only: %i[create destroy]
|
11
|
+
resource :session, only: %i[new create]
|
12
|
+
get 'sign_out', to: 'sessions#destroy'
|
13
|
+
end
|
data/lib/string.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/inflector/methods'
|
4
|
+
|
5
|
+
class String
|
6
|
+
def pluralize(count = nil, locale = :en)
|
7
|
+
case locale
|
8
|
+
when :ru
|
9
|
+
case count
|
10
|
+
when :many
|
11
|
+
I18n.t('cases.')
|
12
|
+
end
|
13
|
+
else
|
14
|
+
locale = count if count.is_a? Symbol
|
15
|
+
count == 1 ? dup : ActiveSupport::Inflector.pluralize(self, locale)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Tramway::Application
|
4
|
+
attr_accessor :name, :url, :model_class, :title, :tagline, :found_date, :phone, :email, :main_image, :favicon,
|
5
|
+
:short_description
|
6
|
+
|
7
|
+
def public_name
|
8
|
+
name.to_s.gsub('_', ' ').camelize
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Tramway::ClassNameHelpers
|
4
|
+
def model_class_name(class_name)
|
5
|
+
class_name.constantize
|
6
|
+
end
|
7
|
+
|
8
|
+
def decorator_class_name(class_name = -> { model_class }.call)
|
9
|
+
"#{class_name}Decorator".constantize
|
10
|
+
end
|
11
|
+
|
12
|
+
def form_class_name(class_name = -> { model_class }.call)
|
13
|
+
"#{class_name}Form".constantize
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'tramway/collections'
|
4
|
+
|
5
|
+
module Tramway::Collections::Helper
|
6
|
+
def collection_list_by(name:)
|
7
|
+
begin
|
8
|
+
require name # needed to load class name with collection
|
9
|
+
rescue LoadError
|
10
|
+
raise "No such file #{name}. You should create file in the `lib/#{name}.rb` or elsewhere you want"
|
11
|
+
end
|
12
|
+
unless ::Tramway::Collection.descendants.map(&:to_s).include?(name.camelize)
|
13
|
+
::Tramway::Error.raise_error(
|
14
|
+
:tramway, :collections, :helper, :collection_list_by, :there_no_such_collection,
|
15
|
+
name_camelize: name.camelize
|
16
|
+
)
|
17
|
+
end
|
18
|
+
|
19
|
+
name.camelize.constantize.list
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'tramway/generators/install_generator'
|
4
|
+
require 'tramway/application'
|
5
|
+
require 'simple_form'
|
6
|
+
require 'enumerize'
|
7
|
+
|
8
|
+
class Tramway::Engine < ::Rails::Engine
|
9
|
+
isolate_namespace Tramway
|
10
|
+
|
11
|
+
config.before_initialize do
|
12
|
+
config.i18n.load_path += Dir["#{config.root}/config/locales/**/*.yml"]
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Tramway::Error < RuntimeError
|
4
|
+
def initialize(*args, plugin: nil, method: nil, message: nil)
|
5
|
+
@properties = {}
|
6
|
+
@properties[:plugin] = plugin
|
7
|
+
@properties[:method] = method
|
8
|
+
@properties[:message] = message
|
9
|
+
super(*args)
|
10
|
+
end
|
11
|
+
|
12
|
+
def message
|
13
|
+
"Plugin: #{@properties[:plugin]}; Method: #{@properties[:method]}; Message: #{@properties[:message]}"
|
14
|
+
end
|
15
|
+
|
16
|
+
def properties
|
17
|
+
@properties ||= {}
|
18
|
+
end
|
19
|
+
|
20
|
+
class << self
|
21
|
+
def raise_error(*coordinates, **options)
|
22
|
+
@errors ||= YAML.load_file("#{Tramway.root}/lib/tramway/yaml/errors.yml").with_indifferent_access
|
23
|
+
error = @errors.dig(*coordinates)
|
24
|
+
raise 'Error is not defined in YAML' unless error
|
25
|
+
|
26
|
+
options.each do |pair|
|
27
|
+
error.gsub!("%{#{pair[0]}}", pair[1].to_s)
|
28
|
+
end
|
29
|
+
raise error
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails/generators'
|
4
|
+
require 'tramway/generators'
|
5
|
+
require 'tramway/generators/model_generator'
|
6
|
+
|
7
|
+
class Tramway::Generators::InstallGenerator < ::Rails::Generators::Base
|
8
|
+
source_root File.expand_path('templates', __dir__)
|
9
|
+
class_option :user_role, type: :string, default: 'admin'
|
10
|
+
include Rails::Generators::Migration
|
11
|
+
|
12
|
+
def run_other_generators
|
13
|
+
generate 'audited:install'
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.next_migration_number(path)
|
17
|
+
next_migration_number = current_migration_number(path) + 1
|
18
|
+
ActiveRecord::Migration.next_migration_number next_migration_number
|
19
|
+
end
|
20
|
+
|
21
|
+
def copy_initializer
|
22
|
+
simple_form_files = %w[simple_form simple_form_bootstrap]
|
23
|
+
simple_form_files.each do |file|
|
24
|
+
copy_file(
|
25
|
+
"/#{File.dirname __dir__}/generators/templates/initializers/#{file}.rb",
|
26
|
+
"config/initializers/#{file}.rb"
|
27
|
+
)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def copy_migrations
|
32
|
+
migrations = %i[
|
33
|
+
create_tramway_users
|
34
|
+
]
|
35
|
+
|
36
|
+
migrations.each do |migration|
|
37
|
+
migration_template "#{migration}.rb", "db/migrate/#{migration}.rb"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def run_decorator_generators
|
42
|
+
Tramway::Error.raise_error(:tramway, :generators, :install) unless Tramway.application.present?
|
43
|
+
|
44
|
+
project = Tramway.application.name
|
45
|
+
Tramway.available_models_for(project).map do |model|
|
46
|
+
generate 'tramway:model', model.to_s, "--user-role=#{options[:user_role]}"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails/generators/named_base'
|
4
|
+
|
5
|
+
class Tramway::Generators::ModelGenerator < ::Rails::Generators::NamedBase
|
6
|
+
source_root File.expand_path('templates', __dir__)
|
7
|
+
class_option :user_role, type: :string, default: 'admin'
|
8
|
+
|
9
|
+
COLLECTION_ATTRIBUTE_LIMIT = 4 # limiting columns in #index table
|
10
|
+
ATTRIBUTE_TYPE_MAPPING = {
|
11
|
+
date: :date_picker,
|
12
|
+
datetime: :date_picker,
|
13
|
+
time: :date_picker,
|
14
|
+
text: :text,
|
15
|
+
string: :string,
|
16
|
+
uuid: :string,
|
17
|
+
float: :float,
|
18
|
+
integer: :integer,
|
19
|
+
boolean: :boolean
|
20
|
+
}.freeze
|
21
|
+
DEFAULT_FIELD_TYPE = :string
|
22
|
+
READ_ONLY_ATTRIBUTES = %w[id uuid created_at updated_at].freeze
|
23
|
+
|
24
|
+
def run_decorator_generator
|
25
|
+
template(
|
26
|
+
'decorator.rb.erb',
|
27
|
+
Rails.root.join("app/decorators/#{file_path}_decorator.rb")
|
28
|
+
)
|
29
|
+
end
|
30
|
+
|
31
|
+
def run_forms_generator
|
32
|
+
template(
|
33
|
+
'form.rb.erb',
|
34
|
+
Rails.root.join("app/forms/#{user_role}/#{file_path}_form.rb")
|
35
|
+
)
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def user_role
|
41
|
+
options[:user_role]
|
42
|
+
end
|
43
|
+
|
44
|
+
def attributes
|
45
|
+
# TODO: this for model associations, but they probably should be handled differently
|
46
|
+
# klass.reflections.keys +
|
47
|
+
|
48
|
+
klass.columns.map(&:name) -
|
49
|
+
redundant_attributes
|
50
|
+
end
|
51
|
+
|
52
|
+
def form_attributes
|
53
|
+
attributes - READ_ONLY_ATTRIBUTES
|
54
|
+
end
|
55
|
+
|
56
|
+
def form_type(attribute)
|
57
|
+
type = column_type_for_attribute(attribute.to_s)
|
58
|
+
|
59
|
+
if type
|
60
|
+
ATTRIBUTE_TYPE_MAPPING.fetch(type, DEFAULT_FIELD_TYPE)
|
61
|
+
# else
|
62
|
+
# association_type(attribute)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def redundant_attributes
|
67
|
+
klass.reflections.keys.flat_map do |relationship|
|
68
|
+
redundant_attributes_for(relationship)
|
69
|
+
end.compact
|
70
|
+
end
|
71
|
+
|
72
|
+
def redundant_attributes_for(relationship)
|
73
|
+
case association_type(relationship)
|
74
|
+
when :polymorphic
|
75
|
+
["#{relationship}_id", "#{relationship}_type"]
|
76
|
+
when :belongs_to
|
77
|
+
"#{relationship}_id"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def association_type(attribute)
|
82
|
+
relationship = klass.reflections[attribute.to_s]
|
83
|
+
if relationship.has_one?
|
84
|
+
:has_one
|
85
|
+
elsif relationship.collection?
|
86
|
+
:has_many
|
87
|
+
elsif relationship.polymorphic?
|
88
|
+
:polymorphic
|
89
|
+
else
|
90
|
+
:belongs_to
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def column_type_for_attribute(attr)
|
95
|
+
column_types(attr)
|
96
|
+
end
|
97
|
+
|
98
|
+
def column_types(attr)
|
99
|
+
klass.columns.find { |column| column.name == attr }.try(:type)
|
100
|
+
end
|
101
|
+
|
102
|
+
def klass
|
103
|
+
@klass ||= Object.const_get(class_name)
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class CreateTramwayUsers < ActiveRecord::Migration[5.1]
|
4
|
+
def change
|
5
|
+
create_table :tramway_users do |t|
|
6
|
+
t.text :email
|
7
|
+
t.text :password_digest
|
8
|
+
t.text :first_name
|
9
|
+
t.text :last_name
|
10
|
+
t.text :avatar
|
11
|
+
t.text :role
|
12
|
+
t.text :phone
|
13
|
+
t.datetime :deleted_at
|
14
|
+
|
15
|
+
t.timestamps null: false
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
class <%= class_name %>Decorator < Tramway::ApplicationDecorator
|
2
|
+
# Associations you want to show in admin dashboard
|
3
|
+
# decorate_associations :messages, :posts
|
4
|
+
|
5
|
+
delegate_attributes(
|
6
|
+
<% attributes.each do |attr| -%>
|
7
|
+
:<%= attr %>,
|
8
|
+
<% end -%>
|
9
|
+
)
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def collections
|
13
|
+
# [ :all, :scope1, :scope2 ]
|
14
|
+
[ :all ]
|
15
|
+
end
|
16
|
+
|
17
|
+
def list_attributes
|
18
|
+
[
|
19
|
+
<% attributes.first(COLLECTION_ATTRIBUTE_LIMIT).each do |attr| -%>
|
20
|
+
:<%= attr %>,
|
21
|
+
<% end -%>
|
22
|
+
]
|
23
|
+
end
|
24
|
+
|
25
|
+
def show_attributes
|
26
|
+
[
|
27
|
+
<% attributes.each do |attr| -%>
|
28
|
+
:<%= attr %>,
|
29
|
+
<% end -%>
|
30
|
+
]
|
31
|
+
end
|
32
|
+
|
33
|
+
def show_associations
|
34
|
+
# Associations you want to show in admin dashboard
|
35
|
+
# [ :messages ]
|
36
|
+
end
|
37
|
+
|
38
|
+
def list_filters
|
39
|
+
# {
|
40
|
+
# filter_name: {
|
41
|
+
# type: :select,
|
42
|
+
# select_collection: filter_collection,
|
43
|
+
# query: lambda do |list, value|
|
44
|
+
# list.where some_attribute: value
|
45
|
+
# end
|
46
|
+
# },
|
47
|
+
# date_filter_name: {
|
48
|
+
# type: :dates,
|
49
|
+
# query: lambda do |list, begin_date, end_date|
|
50
|
+
# list.where 'created_at > ? AND created_at < ?', begin_date, end_date
|
51
|
+
# end
|
52
|
+
# }
|
53
|
+
# }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class <%= user_role.capitalize %>::<%= class_name %>Form < Tramway::ApplicationForm
|
2
|
+
properties <%= form_attributes.map{ |a| ":#{a}" }.join(', ') %>
|
3
|
+
|
4
|
+
def initialize(object)
|
5
|
+
super(object).tap do
|
6
|
+
# Here is the mapping from model attributes to simple_form inputs.
|
7
|
+
# form_properties title: :string,
|
8
|
+
# logo: :file,
|
9
|
+
# description: :ckeditor,
|
10
|
+
# games: :association,
|
11
|
+
# date: :date_picker,
|
12
|
+
# text: :text,
|
13
|
+
# birth_date: {
|
14
|
+
# type: :default,
|
15
|
+
# input_options: {
|
16
|
+
# hint: 'It should be more than 18'
|
17
|
+
# }
|
18
|
+
# }
|
19
|
+
form_properties <%= form_attributes.map{ |attr| "#{attr}: :#{form_type(attr)}"}.join(",\n ") %>
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
SimpleForm.setup do |config|
|
4
|
+
config.wrappers :default, class: :input,
|
5
|
+
hint_class: :field_with_hint, error_class: :field_with_errors do |b|
|
6
|
+
b.use :html5
|
7
|
+
b.use :placeholder
|
8
|
+
b.optional :maxlength
|
9
|
+
b.optional :pattern
|
10
|
+
b.optional :min_max
|
11
|
+
b.optional :readonly
|
12
|
+
b.use :label_input
|
13
|
+
b.use :hint, wrap_with: { tag: :span, class: :hint }
|
14
|
+
b.use :error, wrap_with: { tag: :span, class: :error }
|
15
|
+
end
|
16
|
+
config.default_wrapper = :default
|
17
|
+
config.boolean_style = :nested
|
18
|
+
config.button_class = 'btn'
|
19
|
+
config.error_notification_tag = :div
|
20
|
+
config.error_notification_class = 'error_notification'
|
21
|
+
config.browser_validations = false
|
22
|
+
config.boolean_label_class = 'checkbox'
|
23
|
+
end
|