semantic-administrate 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Rakefile +32 -0
- data/app/assets/fonts/administrate/semantic-ui/icons.eot +0 -0
- data/app/assets/fonts/administrate/semantic-ui/icons.otf +0 -0
- data/app/assets/fonts/administrate/semantic-ui/icons.svg +2671 -0
- data/app/assets/fonts/administrate/semantic-ui/icons.ttf +0 -0
- data/app/assets/fonts/administrate/semantic-ui/icons.woff +0 -0
- data/app/assets/fonts/administrate/semantic-ui/icons.woff2 +0 -0
- data/app/assets/images/administrate/semantic-ui/flags.png +0 -0
- data/app/assets/javascripts/administrate/application.js +7 -0
- data/app/assets/javascripts/administrate/components/date_time_picker.js +6 -0
- data/app/assets/javascripts/administrate/components/has_many_form.js +3 -0
- data/app/assets/javascripts/administrate/components/table.js +23 -0
- data/app/assets/javascripts/administrate/vendor/semantic-ui.js +22943 -0
- data/app/assets/stylesheets/administrate/application.scss +27 -0
- data/app/assets/stylesheets/administrate/base/_forms.scss +106 -0
- data/app/assets/stylesheets/administrate/base/_layout.scss +10 -0
- data/app/assets/stylesheets/administrate/base/_lists.scss +19 -0
- data/app/assets/stylesheets/administrate/base/_typography.scss +48 -0
- data/app/assets/stylesheets/administrate/components/_app-container.scss +27 -0
- data/app/assets/stylesheets/administrate/components/_attributes.scss +34 -0
- data/app/assets/stylesheets/administrate/components/_buttons.scss +43 -0
- data/app/assets/stylesheets/administrate/components/_cells.scss +59 -0
- data/app/assets/stylesheets/administrate/components/_field-unit.scss +36 -0
- data/app/assets/stylesheets/administrate/components/_flashes.scss +28 -0
- data/app/assets/stylesheets/administrate/components/_form-actions.scss +3 -0
- data/app/assets/stylesheets/administrate/components/_main-content.scss +42 -0
- data/app/assets/stylesheets/administrate/components/_pagination.scss +18 -0
- data/app/assets/stylesheets/administrate/components/_search.scss +45 -0
- data/app/assets/stylesheets/administrate/components/nav-drawer.scss +156 -0
- data/app/assets/stylesheets/administrate/library/_clearfix.scss +7 -0
- data/app/assets/stylesheets/administrate/library/_data-label.scss +8 -0
- data/app/assets/stylesheets/administrate/library/_variables.scss +65 -0
- data/app/assets/stylesheets/administrate/vendor/semantic-ui.scss +37123 -0
- data/app/assets/stylesheets/docs.scss +93 -0
- data/app/controllers/administrate/application_controller.rb +162 -0
- data/app/helpers/administrate/application_helper.rb +51 -0
- data/app/views/administrate/application/_collection.html.erb +92 -0
- data/app/views/administrate/application/_flashes.html.erb +20 -0
- data/app/views/administrate/application/_form.html.erb +42 -0
- data/app/views/administrate/application/_icons.erb +13 -0
- data/app/views/administrate/application/_javascript.html.erb +21 -0
- data/app/views/administrate/application/_navigation.html.erb +19 -0
- data/app/views/administrate/application/_navigation_extra_links.html.erb +13 -0
- data/app/views/administrate/application/_navigation_footer.html.erb +44 -0
- data/app/views/administrate/application/_navigation_links.html.erb +16 -0
- data/app/views/administrate/application/_search.html.erb +25 -0
- data/app/views/administrate/application/_stylesheet.html.erb +14 -0
- data/app/views/administrate/application/edit.html.erb +36 -0
- data/app/views/administrate/application/index.html.erb +63 -0
- data/app/views/administrate/application/new.html.erb +32 -0
- data/app/views/administrate/application/show.html.erb +49 -0
- data/app/views/fields/belongs_to/_form.html.erb +26 -0
- data/app/views/fields/belongs_to/_index.html.erb +27 -0
- data/app/views/fields/belongs_to/_show.html.erb +27 -0
- data/app/views/fields/boolean/_form.html.erb +23 -0
- data/app/views/fields/boolean/_index.html.erb +19 -0
- data/app/views/fields/boolean/_show.html.erb +19 -0
- data/app/views/fields/date_time/_form.html.erb +24 -0
- data/app/views/fields/date_time/_index.html.erb +21 -0
- data/app/views/fields/date_time/_show.html.erb +21 -0
- data/app/views/fields/email/_form.html.erb +23 -0
- data/app/views/fields/email/_index.html.erb +18 -0
- data/app/views/fields/email/_show.html.erb +18 -0
- data/app/views/fields/has_many/_form.html.erb +29 -0
- data/app/views/fields/has_many/_index.html.erb +19 -0
- data/app/views/fields/has_many/_show.html.erb +35 -0
- data/app/views/fields/has_one/_form.html.erb +28 -0
- data/app/views/fields/has_one/_index.html.erb +23 -0
- data/app/views/fields/has_one/_show.html.erb +40 -0
- data/app/views/fields/number/_form.html.erb +23 -0
- data/app/views/fields/number/_index.html.erb +19 -0
- data/app/views/fields/number/_show.html.erb +19 -0
- data/app/views/fields/polymorphic/_form.html.erb +32 -0
- data/app/views/fields/polymorphic/_index.html.erb +24 -0
- data/app/views/fields/polymorphic/_show.html.erb +28 -0
- data/app/views/fields/select/_form.html.erb +31 -0
- data/app/views/fields/select/_index.html.erb +16 -0
- data/app/views/fields/select/_show.html.erb +16 -0
- data/app/views/fields/string/_form.html.erb +23 -0
- data/app/views/fields/string/_index.html.erb +18 -0
- data/app/views/fields/string/_show.html.erb +18 -0
- data/app/views/fields/text/_form.html.erb +22 -0
- data/app/views/fields/text/_index.html.erb +18 -0
- data/app/views/fields/text/_show.html.erb +19 -0
- data/app/views/layouts/administrate/application.html.erb +43 -0
- data/config/i18n-tasks.yml +18 -0
- data/config/locales/administrate.ar.yml +26 -0
- data/config/locales/administrate.bs.yml +25 -0
- data/config/locales/administrate.ca.yml +26 -0
- data/config/locales/administrate.da.yml +26 -0
- data/config/locales/administrate.de.yml +26 -0
- data/config/locales/administrate.en.yml +26 -0
- data/config/locales/administrate.es.yml +26 -0
- data/config/locales/administrate.fr.yml +26 -0
- data/config/locales/administrate.it.yml +26 -0
- data/config/locales/administrate.ja.yml +26 -0
- data/config/locales/administrate.ko.yml +26 -0
- data/config/locales/administrate.nl.yml +26 -0
- data/config/locales/administrate.pl.yml +26 -0
- data/config/locales/administrate.pt-BR.yml +27 -0
- data/config/locales/administrate.pt.yml +27 -0
- data/config/locales/administrate.ru.yml +26 -0
- data/config/locales/administrate.sv.yml +26 -0
- data/config/locales/administrate.uk.yml +26 -0
- data/config/locales/administrate.vi.yml +26 -0
- data/config/locales/administrate.zh-CN.yml +26 -0
- data/config/locales/administrate.zh-TW.yml +26 -0
- data/config/routes.rb +2 -0
- data/config/unicorn.rb +30 -0
- data/docs/adding_custom_field_types.md +79 -0
- data/docs/authentication.md +61 -0
- data/docs/customizing_attribute_partials.md +53 -0
- data/docs/customizing_controller_actions.md +42 -0
- data/docs/customizing_dashboards.md +202 -0
- data/docs/customizing_page_views.md +78 -0
- data/docs/getting_started.md +115 -0
- data/lib/administrate.rb +4 -0
- data/lib/administrate/base_dashboard.rb +72 -0
- data/lib/administrate/engine.rb +44 -0
- data/lib/administrate/field/associative.rb +33 -0
- data/lib/administrate/field/base.rb +54 -0
- data/lib/administrate/field/belongs_to.rb +36 -0
- data/lib/administrate/field/boolean.rb +15 -0
- data/lib/administrate/field/date_time.rb +21 -0
- data/lib/administrate/field/deferred.rb +35 -0
- data/lib/administrate/field/email.rb +11 -0
- data/lib/administrate/field/has_many.rb +87 -0
- data/lib/administrate/field/has_one.rb +27 -0
- data/lib/administrate/field/number.rb +35 -0
- data/lib/administrate/field/polymorphic.rb +51 -0
- data/lib/administrate/field/select.rb +21 -0
- data/lib/administrate/field/string.rb +21 -0
- data/lib/administrate/field/text.rb +21 -0
- data/lib/administrate/generator_helpers.rb +13 -0
- data/lib/administrate/namespace.rb +31 -0
- data/lib/administrate/namespace/resource.rb +28 -0
- data/lib/administrate/order.rb +45 -0
- data/lib/administrate/page.rb +4 -0
- data/lib/administrate/page/base.rb +35 -0
- data/lib/administrate/page/collection.rb +33 -0
- data/lib/administrate/page/form.rb +28 -0
- data/lib/administrate/page/show.rb +24 -0
- data/lib/administrate/resource_resolver.rb +43 -0
- data/lib/administrate/search.rb +47 -0
- data/lib/administrate/version.rb +3 -0
- data/lib/administrate/view_generator.rb +36 -0
- data/lib/generators/administrate/assets/assets_generator.rb +12 -0
- data/lib/generators/administrate/assets/javascripts_generator.rb +17 -0
- data/lib/generators/administrate/assets/stylesheets_generator.rb +17 -0
- data/lib/generators/administrate/dashboard/USAGE +9 -0
- data/lib/generators/administrate/dashboard/dashboard_generator.rb +141 -0
- data/lib/generators/administrate/dashboard/templates/controller.rb.erb +21 -0
- data/lib/generators/administrate/dashboard/templates/dashboard.rb.erb +56 -0
- data/lib/generators/administrate/field/field_generator.rb +31 -0
- data/lib/generators/administrate/field/templates/_form.html.erb +6 -0
- data/lib/generators/administrate/field/templates/_index.html.erb +1 -0
- data/lib/generators/administrate/field/templates/_show.html.erb +1 -0
- data/lib/generators/administrate/field/templates/field_object.rb.erb +7 -0
- data/lib/generators/administrate/install/install_generator.rb +49 -0
- data/lib/generators/administrate/install/templates/application_controller.rb.erb +21 -0
- data/lib/generators/administrate/routes/routes_generator.rb +91 -0
- data/lib/generators/administrate/routes/templates/routes.rb.erb +5 -0
- data/lib/generators/administrate/views/edit_generator.rb +16 -0
- data/lib/generators/administrate/views/field_generator.rb +50 -0
- data/lib/generators/administrate/views/form_generator.rb +15 -0
- data/lib/generators/administrate/views/index_generator.rb +16 -0
- data/lib/generators/administrate/views/layout_generator.rb +23 -0
- data/lib/generators/administrate/views/navigation_generator.rb +15 -0
- data/lib/generators/administrate/views/new_generator.rb +16 -0
- data/lib/generators/administrate/views/show_generator.rb +15 -0
- data/lib/generators/administrate/views/views_generator.rb +14 -0
- data/lib/tasks/administrate_tasks.rake +4 -0
- metadata +388 -0
@@ -0,0 +1,35 @@
|
|
1
|
+
module Administrate
|
2
|
+
module Page
|
3
|
+
class Base
|
4
|
+
def initialize(dashboard, options = {})
|
5
|
+
@dashboard = dashboard
|
6
|
+
@options = options
|
7
|
+
end
|
8
|
+
|
9
|
+
def resource_name
|
10
|
+
@resource_name ||=
|
11
|
+
dashboard.class.to_s.scan(/(.+)Dashboard/).first.first.underscore
|
12
|
+
end
|
13
|
+
|
14
|
+
def resource_path
|
15
|
+
@resource_path ||= resource_name.gsub("/", "_")
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
def attribute_field(dashboard, resource, attribute_name, page)
|
21
|
+
value = get_attribute_value(resource, attribute_name)
|
22
|
+
field = dashboard.attribute_type_for(attribute_name)
|
23
|
+
field.new(attribute_name, value, page, resource: resource)
|
24
|
+
end
|
25
|
+
|
26
|
+
def get_attribute_value(resource, attribute_name)
|
27
|
+
resource.public_send(attribute_name)
|
28
|
+
rescue NameError
|
29
|
+
nil
|
30
|
+
end
|
31
|
+
|
32
|
+
attr_reader :dashboard, :options
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require_relative "base"
|
2
|
+
|
3
|
+
module Administrate
|
4
|
+
module Page
|
5
|
+
class Collection < Page::Base
|
6
|
+
def attribute_names
|
7
|
+
dashboard.collection_attributes
|
8
|
+
end
|
9
|
+
|
10
|
+
def attributes_for(resource)
|
11
|
+
attribute_names.map do |attr_name|
|
12
|
+
attribute_field(dashboard, resource, attr_name, :index)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def attribute_types
|
17
|
+
dashboard.attribute_types_for(attribute_names)
|
18
|
+
end
|
19
|
+
|
20
|
+
def ordered_html_class(attr)
|
21
|
+
ordered_by?(attr) && order.direction
|
22
|
+
end
|
23
|
+
|
24
|
+
delegate :ordered_by?, :order_params_for, to: :order
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def order
|
29
|
+
options[:order] || Order.new
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative "base"
|
2
|
+
|
3
|
+
module Administrate
|
4
|
+
module Page
|
5
|
+
class Form < Page::Base
|
6
|
+
def initialize(dashboard, resource)
|
7
|
+
super(dashboard)
|
8
|
+
@resource = resource
|
9
|
+
end
|
10
|
+
|
11
|
+
attr_reader :resource
|
12
|
+
|
13
|
+
def attributes
|
14
|
+
dashboard.form_attributes.map do |attribute|
|
15
|
+
attribute_field(dashboard, resource, attribute, :form)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def page_title
|
20
|
+
dashboard.display_resource(resource)
|
21
|
+
end
|
22
|
+
|
23
|
+
protected
|
24
|
+
|
25
|
+
attr_reader :dashboard
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require_relative "base"
|
2
|
+
|
3
|
+
module Administrate
|
4
|
+
module Page
|
5
|
+
class Show < Page::Base
|
6
|
+
def initialize(dashboard, resource)
|
7
|
+
super(dashboard)
|
8
|
+
@resource = resource
|
9
|
+
end
|
10
|
+
|
11
|
+
attr_reader :resource
|
12
|
+
|
13
|
+
def page_title
|
14
|
+
dashboard.display_resource(resource)
|
15
|
+
end
|
16
|
+
|
17
|
+
def attributes
|
18
|
+
dashboard.show_page_attributes.map do |attr_name|
|
19
|
+
attribute_field(dashboard, resource, attr_name, :show)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Administrate
|
2
|
+
class ResourceResolver
|
3
|
+
def initialize(controller_path)
|
4
|
+
@controller_path = controller_path
|
5
|
+
end
|
6
|
+
|
7
|
+
def dashboard_class
|
8
|
+
ActiveSupport::Inflector.constantize("#{resource_class_name}Dashboard")
|
9
|
+
end
|
10
|
+
|
11
|
+
def namespace
|
12
|
+
controller_path.split("/").first
|
13
|
+
end
|
14
|
+
|
15
|
+
def resource_class
|
16
|
+
ActiveSupport::Inflector.constantize(resource_class_name)
|
17
|
+
end
|
18
|
+
|
19
|
+
def resource_name
|
20
|
+
model_path_parts.map(&:underscore).join("__").to_sym
|
21
|
+
end
|
22
|
+
|
23
|
+
def resource_title
|
24
|
+
model_path_parts.join(" ")
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def resource_class_name
|
30
|
+
model_path_parts.join("::")
|
31
|
+
end
|
32
|
+
|
33
|
+
def model_path_parts
|
34
|
+
controller_path_parts.map(&:camelize)
|
35
|
+
end
|
36
|
+
|
37
|
+
def controller_path_parts
|
38
|
+
controller_path.split("/")[1..-1].map(&:singularize)
|
39
|
+
end
|
40
|
+
|
41
|
+
attr_reader :controller_path
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require "active_support/core_ext/module/delegation"
|
2
|
+
require "active_support/core_ext/object/blank"
|
3
|
+
|
4
|
+
module Administrate
|
5
|
+
class Search
|
6
|
+
def initialize(scoped_resource, dashboard_class, term)
|
7
|
+
@dashboard_class = dashboard_class
|
8
|
+
@scoped_resource = scoped_resource
|
9
|
+
@term = term
|
10
|
+
end
|
11
|
+
|
12
|
+
def run
|
13
|
+
if @term.blank?
|
14
|
+
@scoped_resource.all
|
15
|
+
else
|
16
|
+
@scoped_resource.where(query, *search_terms)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def query
|
23
|
+
search_attributes.map do |attr|
|
24
|
+
table_name = ActiveRecord::Base.connection.
|
25
|
+
quote_table_name(@scoped_resource.table_name)
|
26
|
+
attr_name = ActiveRecord::Base.connection.quote_column_name(attr)
|
27
|
+
"lower(#{table_name}.#{attr_name}) LIKE ?"
|
28
|
+
end.join(" OR ")
|
29
|
+
end
|
30
|
+
|
31
|
+
def search_terms
|
32
|
+
["%#{term.mb_chars.downcase}%"] * search_attributes.count
|
33
|
+
end
|
34
|
+
|
35
|
+
def search_attributes
|
36
|
+
attribute_types.keys.select do |attribute|
|
37
|
+
attribute_types[attribute].searchable?
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def attribute_types
|
42
|
+
@dashboard_class::ATTRIBUTE_TYPES
|
43
|
+
end
|
44
|
+
|
45
|
+
attr_reader :resolver, :term
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require "rails/generators/base"
|
2
|
+
require "administrate/generator_helpers"
|
3
|
+
|
4
|
+
module Administrate
|
5
|
+
class ViewGenerator < Rails::Generators::Base
|
6
|
+
include Administrate::GeneratorHelpers
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def self.template_source_path
|
11
|
+
File.expand_path(
|
12
|
+
"../../../app/views/administrate/application",
|
13
|
+
__FILE__,
|
14
|
+
)
|
15
|
+
end
|
16
|
+
|
17
|
+
def copy_resource_template(template_name)
|
18
|
+
template_file = "#{template_name}.html.erb"
|
19
|
+
|
20
|
+
copy_file(
|
21
|
+
template_file,
|
22
|
+
"app/views/admin/#{resource_path}/#{template_file}",
|
23
|
+
)
|
24
|
+
end
|
25
|
+
|
26
|
+
def resource_path
|
27
|
+
args.first.try(:underscore).try(:pluralize) || BaseResourcePath.new
|
28
|
+
end
|
29
|
+
|
30
|
+
class BaseResourcePath
|
31
|
+
def to_s
|
32
|
+
"application"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require "administrate/view_generator"
|
2
|
+
|
3
|
+
module Administrate
|
4
|
+
module Generators
|
5
|
+
class AssetsGenerator < Administrate::ViewGenerator
|
6
|
+
def copy_assets
|
7
|
+
call_generator("administrate:assets:javascripts")
|
8
|
+
call_generator("administrate:assets:stylesheets")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "rails/generators/base"
|
2
|
+
|
3
|
+
module Administrate
|
4
|
+
module Generators
|
5
|
+
module Assets
|
6
|
+
class JavascriptsGenerator < Rails::Generators::Base
|
7
|
+
JAVASCRIPTS_PATH = "app/assets/javascripts/administrate"
|
8
|
+
|
9
|
+
source_root File.expand_path("../../../../../", __FILE__)
|
10
|
+
|
11
|
+
def copy_javascripts
|
12
|
+
directory JAVASCRIPTS_PATH, JAVASCRIPTS_PATH
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "rails/generators/base"
|
2
|
+
|
3
|
+
module Administrate
|
4
|
+
module Generators
|
5
|
+
module Assets
|
6
|
+
class StylesheetsGenerator < Rails::Generators::Base
|
7
|
+
STYLESHEETS_PATH = "app/assets/stylesheets/administrate"
|
8
|
+
|
9
|
+
source_root File.expand_path("../../../../../", __FILE__)
|
10
|
+
|
11
|
+
def copy_stylesheets
|
12
|
+
directory STYLESHEETS_PATH, STYLESHEETS_PATH
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,141 @@
|
|
1
|
+
require "rails/generators/named_base"
|
2
|
+
|
3
|
+
module Administrate
|
4
|
+
module Generators
|
5
|
+
class DashboardGenerator < Rails::Generators::NamedBase
|
6
|
+
ATTRIBUTE_TYPE_MAPPING = {
|
7
|
+
boolean: "Field::Boolean",
|
8
|
+
date: "Field::DateTime",
|
9
|
+
datetime: "Field::DateTime",
|
10
|
+
enum: "Field::String",
|
11
|
+
float: "Field::Number",
|
12
|
+
integer: "Field::Number",
|
13
|
+
time: "Field::DateTime",
|
14
|
+
text: "Field::Text",
|
15
|
+
string: "Field::String",
|
16
|
+
}
|
17
|
+
|
18
|
+
ATTRIBUTE_OPTIONS_MAPPING = {
|
19
|
+
enum: { searchable: false },
|
20
|
+
float: { decimals: 2 },
|
21
|
+
}
|
22
|
+
|
23
|
+
DEFAULT_FIELD_TYPE = "Field::String.with_options(searchable: false)"
|
24
|
+
COLLECTION_ATTRIBUTE_LIMIT = 4
|
25
|
+
READ_ONLY_ATTRIBUTES = %w[id created_at updated_at]
|
26
|
+
|
27
|
+
class_option :namespace, type: :string, default: "admin"
|
28
|
+
|
29
|
+
source_root File.expand_path("../templates", __FILE__)
|
30
|
+
|
31
|
+
def create_dashboard_definition
|
32
|
+
template(
|
33
|
+
"dashboard.rb.erb",
|
34
|
+
Rails.root.join("app/dashboards/#{file_name}_dashboard.rb"),
|
35
|
+
)
|
36
|
+
end
|
37
|
+
|
38
|
+
def create_resource_controller
|
39
|
+
destination = Rails.root.join(
|
40
|
+
"app/controllers/#{namespace}/#{file_name.pluralize}_controller.rb",
|
41
|
+
)
|
42
|
+
|
43
|
+
template("controller.rb.erb", destination)
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def namespace
|
49
|
+
options[:namespace]
|
50
|
+
end
|
51
|
+
|
52
|
+
def attributes
|
53
|
+
klass.reflections.keys + klass.attribute_names - redundant_attributes
|
54
|
+
end
|
55
|
+
|
56
|
+
def form_attributes
|
57
|
+
attributes - READ_ONLY_ATTRIBUTES
|
58
|
+
end
|
59
|
+
|
60
|
+
def redundant_attributes
|
61
|
+
klass.reflections.keys.flat_map do |relationship|
|
62
|
+
redundant_attributes_for(relationship)
|
63
|
+
end.compact
|
64
|
+
end
|
65
|
+
|
66
|
+
def redundant_attributes_for(relationship)
|
67
|
+
case association_type(relationship)
|
68
|
+
when "Field::Polymorphic"
|
69
|
+
[relationship + "_id", relationship + "_type"]
|
70
|
+
when "Field::BelongsTo"
|
71
|
+
relationship + "_id"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def field_type(attribute)
|
76
|
+
type = column_type_for_attribute(attribute.to_s)
|
77
|
+
|
78
|
+
if type
|
79
|
+
ATTRIBUTE_TYPE_MAPPING.fetch(type, DEFAULT_FIELD_TYPE) +
|
80
|
+
options_string(ATTRIBUTE_OPTIONS_MAPPING.fetch(type, {}))
|
81
|
+
else
|
82
|
+
association_type(attribute)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def column_type_for_attribute(attr)
|
87
|
+
if enum_column?(attr)
|
88
|
+
:enum
|
89
|
+
else
|
90
|
+
column_types(attr)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def enum_column?(attr)
|
95
|
+
klass.respond_to?(:defined_enums) &&
|
96
|
+
klass.defined_enums.keys.include?(attr)
|
97
|
+
end
|
98
|
+
|
99
|
+
def column_types(attr)
|
100
|
+
klass.columns.find { |column| column.name == attr }.try(:type)
|
101
|
+
end
|
102
|
+
|
103
|
+
def association_type(attribute)
|
104
|
+
relationship = klass.reflections[attribute.to_s]
|
105
|
+
if relationship.has_one?
|
106
|
+
"Field::HasOne"
|
107
|
+
elsif relationship.collection?
|
108
|
+
"Field::HasMany" + relationship_options_string(relationship)
|
109
|
+
elsif relationship.polymorphic?
|
110
|
+
"Field::Polymorphic"
|
111
|
+
else
|
112
|
+
"Field::BelongsTo" + relationship_options_string(relationship)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def klass
|
117
|
+
@klass ||= Object.const_get(class_name)
|
118
|
+
end
|
119
|
+
|
120
|
+
def relationship_options_string(relationship)
|
121
|
+
if relationship.class_name != relationship.name.to_s.classify
|
122
|
+
options_string(class_name: relationship.class_name)
|
123
|
+
else
|
124
|
+
""
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def options_string(options)
|
129
|
+
if options.any?
|
130
|
+
".with_options(#{inspect_hash_as_ruby(options)})"
|
131
|
+
else
|
132
|
+
""
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def inspect_hash_as_ruby(hash)
|
137
|
+
hash.map { |key, value| "#{key}: #{value.inspect}" }.join(", ")
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|