admin_core 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitattributes +2 -0
- data/.gitignore +53 -0
- data/.rspec +2 -0
- data/.rubocop.yml +27 -0
- data/CHANGELOG.md +15 -0
- data/Gemfile +3 -0
- data/README.md +48 -0
- data/Rakefile +31 -0
- data/admin_core.gemspec +32 -0
- data/client/.babelrc +19 -0
- data/client/.eslintignore +3 -0
- data/client/.eslintrc.yml +20 -0
- data/client/.flowconfig +7 -0
- data/client/.gitignore +64 -0
- data/client/README.md +3 -0
- data/client/admin-core.scss +8 -0
- data/client/flow-typed/npm/axios_v0.16.x.js +120 -0
- data/client/flow-typed/npm/classnames_v2.x.x.js +16 -0
- data/client/flow-typed/npm/lodash_v4.x.x.js +514 -0
- data/client/flow-typed/npm/react-router-dom_v4.x.x.js +166 -0
- data/client/flow-typed/npm/reactstrap_vx.x.x.js +536 -0
- data/client/package.json +60 -0
- data/client/src/.eslintrc.yml +23 -0
- data/client/src/AdminCore.jsx +44 -0
- data/client/src/components/Breadcrumb.jsx +18 -0
- data/client/src/components/Header.jsx +45 -0
- data/client/src/components/Pagination.jsx +72 -0
- data/client/src/components/ResourceFilters.jsx +87 -0
- data/client/src/components/ResourceForm.jsx +103 -0
- data/client/src/components/ResourcesCollection.jsx +41 -0
- data/client/src/components/Sidebar.jsx +90 -0
- data/client/src/decls.js +119 -0
- data/client/src/http-client.js +18 -0
- data/client/src/main.js +9 -0
- data/client/src/resource-field/BelongsTo.jsx +26 -0
- data/client/src/resource-field/Boolean.jsx +43 -0
- data/client/src/resource-field/Date.jsx +29 -0
- data/client/src/resource-field/DateTime.jsx +29 -0
- data/client/src/resource-field/Enum.jsx +34 -0
- data/client/src/resource-field/Number.jsx +28 -0
- data/client/src/resource-field/String.jsx +28 -0
- data/client/src/resource-field/Text.jsx +27 -0
- data/client/src/resource-field-renderer.js +45 -0
- data/client/src/resource-filter/Boolean.jsx +22 -0
- data/client/src/resource-filter/Number.jsx +45 -0
- data/client/src/resource-filter/String.jsx +46 -0
- data/client/src/resource-filter-renderer.js +17 -0
- data/client/src/resource-page/Base.js +36 -0
- data/client/src/resource-page/Edit.jsx +48 -0
- data/client/src/resource-page/Index.jsx +141 -0
- data/client/src/resource-page/New.jsx +48 -0
- data/client/src/resource-page/Show.jsx +116 -0
- data/client/webpack.config.js +26 -0
- data/client/yarn.lock +3816 -0
- data/lib/admin_core/base_controller.rb +114 -0
- data/lib/admin_core/base_resource_manager.rb +24 -0
- data/lib/admin_core/configuration.rb +20 -0
- data/lib/admin_core/engine.rb +6 -0
- data/lib/admin_core/errors.rb +17 -0
- data/lib/admin_core/resource_field/base.rb +69 -0
- data/lib/admin_core/resource_field/belongs_to.rb +38 -0
- data/lib/admin_core/resource_field/boolean.rb +18 -0
- data/lib/admin_core/resource_field/date.rb +18 -0
- data/lib/admin_core/resource_field/date_time.rb +18 -0
- data/lib/admin_core/resource_field/enum.rb +26 -0
- data/lib/admin_core/resource_field/number.rb +18 -0
- data/lib/admin_core/resource_field/string.rb +18 -0
- data/lib/admin_core/resource_field/text.rb +23 -0
- data/lib/admin_core/resource_field_builder.rb +48 -0
- data/lib/admin_core/resource_filter/base.rb +63 -0
- data/lib/admin_core/resource_filter/boolean.rb +17 -0
- data/lib/admin_core/resource_filter/number.rb +25 -0
- data/lib/admin_core/resource_filter/string.rb +27 -0
- data/lib/admin_core/resource_filter_builder.rb +37 -0
- data/lib/admin_core/resource_manager/buildable.rb +42 -0
- data/lib/admin_core/resource_manager/convert.rb +95 -0
- data/lib/admin_core/resource_manager/has_many_fields.rb +71 -0
- data/lib/admin_core/resource_manager/permission.rb +35 -0
- data/lib/admin_core/resource_manager/searchable.rb +57 -0
- data/lib/admin_core/resource_page/base.rb +17 -0
- data/lib/admin_core/resource_page/edit.rb +22 -0
- data/lib/admin_core/resource_page/index.rb +52 -0
- data/lib/admin_core/resource_page/new.rb +26 -0
- data/lib/admin_core/resource_page/show.rb +22 -0
- data/lib/admin_core/resource_router.rb +58 -0
- data/lib/admin_core/resource_search.rb +22 -0
- data/lib/admin_core/rspec/matchers.rb +18 -0
- data/lib/admin_core/rspec/resource_field_spec_helper.rb +54 -0
- data/lib/admin_core/version.rb +11 -0
- data/lib/admin_core/view_object/sidebar_dropdown.rb +21 -0
- data/lib/admin_core/view_object/sidebar_link.rb +24 -0
- data/lib/admin_core/view_object/sidebar_resource_link.rb +23 -0
- data/lib/admin_core/view_object/sidebar_title.rb +18 -0
- data/lib/admin_core.rb +69 -0
- data/lib/generators/admin_core/install_generator.rb +39 -0
- data/lib/generators/admin_core/resource_manager_generator.rb +166 -0
- data/lib/generators/admin_core/templates/admin-core.css +1 -0
- data/lib/generators/admin_core/templates/admin-core.js +38196 -0
- data/lib/generators/admin_core/templates/controller.rb.erb +4 -0
- data/lib/generators/admin_core/templates/initializer.rb.erb +3 -0
- data/lib/generators/admin_core/templates/resource_manager.rb.erb +33 -0
- data/lib/generators/admin_core/templates/view.html.erb +58 -0
- data/sample/.gitignore +21 -0
- data/sample/Gemfile +35 -0
- data/sample/Gemfile.lock +147 -0
- data/sample/README.md +24 -0
- data/sample/Rakefile +6 -0
- data/sample/app/assets/config/manifest.js +2 -0
- data/sample/app/assets/images/.keep +0 -0
- data/sample/app/assets/stylesheets/application.css +15 -0
- data/sample/app/controllers/admin/application_controller.rb +4 -0
- data/sample/app/controllers/admin/tweets_controller.rb +4 -0
- data/sample/app/controllers/admin/users_controller.rb +4 -0
- data/sample/app/controllers/application_controller.rb +3 -0
- data/sample/app/controllers/concerns/.keep +0 -0
- data/sample/app/helpers/application_helper.rb +2 -0
- data/sample/app/jobs/application_job.rb +2 -0
- data/sample/app/models/admin/tweet.rb +35 -0
- data/sample/app/models/admin/user.rb +41 -0
- data/sample/app/models/application_record.rb +3 -0
- data/sample/app/models/concerns/.keep +0 -0
- data/sample/app/models/tweet.rb +3 -0
- data/sample/app/models/user.rb +3 -0
- data/sample/app/views/admin/application.html.erb +64 -0
- data/sample/app/views/layouts/application.html.erb +13 -0
- data/sample/bin/bundle +3 -0
- data/sample/bin/rails +4 -0
- data/sample/bin/rake +4 -0
- data/sample/bin/setup +34 -0
- data/sample/bin/update +29 -0
- data/sample/config/application.rb +25 -0
- data/sample/config/boot.rb +3 -0
- data/sample/config/database.yml +25 -0
- data/sample/config/environment.rb +5 -0
- data/sample/config/environments/development.rb +42 -0
- data/sample/config/environments/production.rb +69 -0
- data/sample/config/environments/test.rb +36 -0
- data/sample/config/initializers/admin_core.rb +8 -0
- data/sample/config/initializers/application_controller_renderer.rb +6 -0
- data/sample/config/initializers/backtrace_silencers.rb +7 -0
- data/sample/config/initializers/cookies_serializer.rb +5 -0
- data/sample/config/initializers/filter_parameter_logging.rb +4 -0
- data/sample/config/initializers/inflections.rb +16 -0
- data/sample/config/initializers/mime_types.rb +4 -0
- data/sample/config/initializers/new_framework_defaults.rb +24 -0
- data/sample/config/initializers/session_store.rb +3 -0
- data/sample/config/initializers/wrap_parameters.rb +14 -0
- data/sample/config/locales/en.yml +23 -0
- data/sample/config/routes.rb +6 -0
- data/sample/config/secrets.yml +22 -0
- data/sample/config.ru +5 -0
- data/sample/db/migrate/20170417055257_create_users.rb +10 -0
- data/sample/db/migrate/20170417055412_create_tweets.rb +9 -0
- data/sample/db/schema.rb +31 -0
- data/sample/db/seeds.rb +7 -0
- data/sample/lib/assets/.keep +0 -0
- data/sample/lib/tasks/.keep +0 -0
- data/sample/log/.keep +0 -0
- data/sample/public/404.html +67 -0
- data/sample/public/422.html +67 -0
- data/sample/public/500.html +66 -0
- data/sample/public/apple-touch-icon-precomposed.png +0 -0
- data/sample/public/apple-touch-icon.png +0 -0
- data/sample/public/bundle.min.js +27 -0
- data/sample/public/bundle.min.js.map +1 -0
- data/sample/public/favicon.ico +0 -0
- data/sample/public/javascripts/admin-core.js +38196 -0
- data/sample/public/robots.txt +5 -0
- data/sample/public/stylesheets/admin-core.css +1 -0
- data/sample/tmp/.keep +0 -0
- data/sample/vendor/assets/stylesheets/.keep +0 -0
- metadata +368 -0
@@ -0,0 +1,18 @@
|
|
1
|
+
module AdminCore
|
2
|
+
module ViewObject
|
3
|
+
class SidebarTitle
|
4
|
+
# @param name [String]
|
5
|
+
def initialize(name)
|
6
|
+
@name = name
|
7
|
+
end
|
8
|
+
|
9
|
+
# @note Implements SidebarTitle flow type
|
10
|
+
def to_hash
|
11
|
+
{
|
12
|
+
displayName: @name,
|
13
|
+
type: 'title'
|
14
|
+
}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/admin_core.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
module AdminCore
|
2
|
+
class << self
|
3
|
+
# @return [AdminCore::Configuration]
|
4
|
+
def config
|
5
|
+
@configuration ||= AdminCore::Configuration.new
|
6
|
+
end
|
7
|
+
|
8
|
+
# @yield [AdminCore::Configuration]
|
9
|
+
# @example
|
10
|
+
# AdminCore.configure do |config|
|
11
|
+
# config.route_name_prefix = "admin_core"
|
12
|
+
# end
|
13
|
+
def configure
|
14
|
+
yield config if block_given?
|
15
|
+
end
|
16
|
+
|
17
|
+
# @param manager_class [Class]
|
18
|
+
def register_resource_manager(manager_class)
|
19
|
+
if resource_managers.include?(manager_class)
|
20
|
+
raise AdminCore::ResourceManagerAlreadyRegistered, manager_class.resource_name
|
21
|
+
end
|
22
|
+
resource_managers.push(manager_class)
|
23
|
+
end
|
24
|
+
|
25
|
+
# @return [Array<Class>]
|
26
|
+
def resource_managers
|
27
|
+
@resource_managers ||= []
|
28
|
+
end
|
29
|
+
|
30
|
+
# @param field_class [Class] a child of {AdminCore::ResourceField::Base}
|
31
|
+
def register_resource_field(field_class)
|
32
|
+
if resource_field_map.key?(field_class.type)
|
33
|
+
raise AdminCore::ResourceFilterAlreadyRegistered, field_class.type
|
34
|
+
end
|
35
|
+
resource_field_map[field_class.type] = field_class
|
36
|
+
end
|
37
|
+
|
38
|
+
# @return [Hash<Symbol, Class>]
|
39
|
+
def resource_field_map
|
40
|
+
@resource_fields ||= {}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
require 'kaminari'
|
46
|
+
|
47
|
+
require 'admin_core/base_controller'
|
48
|
+
require 'admin_core/base_resource_manager'
|
49
|
+
require 'admin_core/configuration'
|
50
|
+
require 'admin_core/engine'
|
51
|
+
require 'admin_core/resource_field/belongs_to'
|
52
|
+
require 'admin_core/resource_field/boolean'
|
53
|
+
require 'admin_core/resource_field/date'
|
54
|
+
require 'admin_core/resource_field/date_time'
|
55
|
+
require 'admin_core/resource_field/enum'
|
56
|
+
require 'admin_core/resource_field/number'
|
57
|
+
require 'admin_core/resource_field/string'
|
58
|
+
require 'admin_core/resource_field/text'
|
59
|
+
require 'admin_core/resource_filter/boolean'
|
60
|
+
require 'admin_core/resource_filter/number'
|
61
|
+
require 'admin_core/resource_filter/string'
|
62
|
+
require 'admin_core/resource_page/edit'
|
63
|
+
require 'admin_core/resource_page/index'
|
64
|
+
require 'admin_core/resource_page/new'
|
65
|
+
require 'admin_core/resource_page/show'
|
66
|
+
require 'admin_core/view_object/sidebar_dropdown'
|
67
|
+
require 'admin_core/view_object/sidebar_link'
|
68
|
+
require 'admin_core/view_object/sidebar_resource_link'
|
69
|
+
require 'admin_core/view_object/sidebar_title'
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'admin_core/configuration'
|
2
|
+
|
3
|
+
module AdminCore
|
4
|
+
module Generators
|
5
|
+
class InstallGenerator < Rails::Generators::Base
|
6
|
+
source_root File.expand_path('../templates', __FILE__)
|
7
|
+
|
8
|
+
class_option :module_name, type: :string,
|
9
|
+
default: AdminCore::Configuration::DEFAULT_MODULE_NAME,
|
10
|
+
desc: 'Module which contains resource manager, controllers, etc.',
|
11
|
+
aliases: :m
|
12
|
+
|
13
|
+
def initializer
|
14
|
+
template('initializer.rb.erb', 'config/initializers/admin_core.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
def view
|
18
|
+
copy_file('view.html.erb', "app/views/#{configuration.template}.html.erb")
|
19
|
+
end
|
20
|
+
|
21
|
+
def controller
|
22
|
+
template('controller.rb.erb', "app/controllers/#{configuration.route_name_prefix}/application_controller.rb")
|
23
|
+
end
|
24
|
+
|
25
|
+
def assets
|
26
|
+
copy_file('admin-core.js', 'public/javascripts/admin-core.js')
|
27
|
+
copy_file('admin-core.css', 'public/stylesheets/admin-core.css')
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def configuration
|
33
|
+
@configuration ||= AdminCore::Configuration.new.tap do |config|
|
34
|
+
config.module_name = options[:module_name].camelize
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,166 @@
|
|
1
|
+
module AdminCore
|
2
|
+
module Generators
|
3
|
+
class ResourceManagerGenerator < Rails::Generators::NamedBase
|
4
|
+
source_root File.expand_path('../templates', __FILE__)
|
5
|
+
|
6
|
+
class_option :module_name, type: :string,
|
7
|
+
default: AdminCore::Configuration::DEFAULT_MODULE_NAME,
|
8
|
+
desc: 'Module which contains resource manager, controllers, etc.',
|
9
|
+
aliases: :m
|
10
|
+
|
11
|
+
def generate_resource_manager_file
|
12
|
+
template('resource_manager.rb.erb', "app/models/#{configuration.route_name_prefix}/#{file_name}.rb")
|
13
|
+
end
|
14
|
+
|
15
|
+
def register_resource_manager
|
16
|
+
File.open('config/initializers/admin_core.rb', 'a') do |file|
|
17
|
+
file.puts("AdminCore.register_resource_manager(#{configuration.module_name}::#{class_name})")
|
18
|
+
end
|
19
|
+
puts ' updated config/initializers/admin_core.rb'
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
class Attribute
|
25
|
+
READONLY_ATTRIBUTE_NAMES = %w[id created_at updated_at].freeze
|
26
|
+
|
27
|
+
FIELD_MAPPING = Hash.new { |_, k| k }.merge(
|
28
|
+
datetime: :date_time,
|
29
|
+
integer: :number,
|
30
|
+
time: :date_time
|
31
|
+
)
|
32
|
+
|
33
|
+
FILTER_MAPPING = Hash.new { |_, k| k }.merge(
|
34
|
+
text: :string
|
35
|
+
)
|
36
|
+
|
37
|
+
attr_reader :name
|
38
|
+
|
39
|
+
def initialize(resource_class, name)
|
40
|
+
@resource_class = resource_class
|
41
|
+
@name = name
|
42
|
+
end
|
43
|
+
|
44
|
+
def define_field
|
45
|
+
if field_implemented?
|
46
|
+
"define_field :#{name}, :#{field_type}"
|
47
|
+
else
|
48
|
+
"# define_field :#{name}, :#{field_type}"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def define_and_register_filter
|
53
|
+
"define_and_register_filter :#{name}, :#{filter_type}"
|
54
|
+
end
|
55
|
+
|
56
|
+
def register_name
|
57
|
+
field_implemented? ? ":#{name}," : "# :#{name},"
|
58
|
+
end
|
59
|
+
|
60
|
+
def form?
|
61
|
+
READONLY_ATTRIBUTE_NAMES.exclude?(name)
|
62
|
+
end
|
63
|
+
|
64
|
+
def field_implemented?
|
65
|
+
AdminCore.resource_field_map.key?(field_type)
|
66
|
+
end
|
67
|
+
|
68
|
+
def filter_implemented?
|
69
|
+
%i[boolean number string].include?(filter_type)
|
70
|
+
end
|
71
|
+
|
72
|
+
# @return [Boolean]
|
73
|
+
def redundant?
|
74
|
+
case field_type
|
75
|
+
when :string
|
76
|
+
redundant_string?
|
77
|
+
when :number
|
78
|
+
redundant_number?
|
79
|
+
else
|
80
|
+
false
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
private
|
85
|
+
|
86
|
+
attr_reader :resource_class
|
87
|
+
|
88
|
+
def redundant_string?
|
89
|
+
name.to_s.end_with?('_type') && resource_class.reflections[name.to_s[0...-4]]
|
90
|
+
end
|
91
|
+
|
92
|
+
def redundant_number?
|
93
|
+
name.to_s.end_with?('_id') && resource_class.reflections[name.to_s[0...-3]]
|
94
|
+
end
|
95
|
+
|
96
|
+
# @return [Symbol]
|
97
|
+
def field_type
|
98
|
+
FIELD_MAPPING[attribute_type]
|
99
|
+
end
|
100
|
+
|
101
|
+
# @return [Symbol]
|
102
|
+
def filter_type
|
103
|
+
FILTER_MAPPING[attribute_type]
|
104
|
+
end
|
105
|
+
|
106
|
+
# @return [Symbol]
|
107
|
+
def attribute_type
|
108
|
+
return :enum if enum_type?
|
109
|
+
return association_type if association_type?
|
110
|
+
resource_class.column_for_attribute(name).type
|
111
|
+
end
|
112
|
+
|
113
|
+
def enum_type?
|
114
|
+
enum_attribute_names.include?(name)
|
115
|
+
end
|
116
|
+
|
117
|
+
def association_type?
|
118
|
+
resource_class.reflections.key?(name)
|
119
|
+
end
|
120
|
+
|
121
|
+
# @return [Symbol]
|
122
|
+
def association_type
|
123
|
+
reflection = resource_class.reflections[name]
|
124
|
+
if reflection.has_one?
|
125
|
+
:has_one
|
126
|
+
elsif reflection.collection?
|
127
|
+
:has_many
|
128
|
+
elsif reflection.polymorphic?
|
129
|
+
:polymorphic
|
130
|
+
else
|
131
|
+
:belongs_to
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
# @return [Array<String>]
|
136
|
+
def enum_attribute_names
|
137
|
+
@enum_attribute_names ||= resource_class.try(:defined_enums).try(:keys) || []
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def configuration
|
142
|
+
@configuration ||= AdminCore::Configuration.new.tap do |config|
|
143
|
+
config.module_name = options[:module_name].camelize
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
# @return [Array<Attribute>]
|
148
|
+
def attributes
|
149
|
+
@attributes ||= attribute_names.map(&method(:build_attribute)).reject(&:redundant?)
|
150
|
+
end
|
151
|
+
|
152
|
+
# @return [Array<String>]
|
153
|
+
def attribute_names
|
154
|
+
resource_class.attribute_names + resource_class.reflections.keys
|
155
|
+
end
|
156
|
+
|
157
|
+
def resource_class
|
158
|
+
@resource_class ||= Object.const_get(class_name)
|
159
|
+
end
|
160
|
+
|
161
|
+
def build_attribute(attribute_name)
|
162
|
+
Attribute.new(resource_class, attribute_name)
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|