super 0.0.3 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +11 -0
  3. data/CONTRIBUTING.md +56 -0
  4. data/README.md +65 -76
  5. data/STABILITY.md +50 -0
  6. data/app/assets/javascripts/super/application.js +1186 -11151
  7. data/app/assets/stylesheets/super/application.css +93514 -27381
  8. data/app/controllers/super/application_controller.rb +49 -72
  9. data/app/views/layouts/super/application.html.erb +24 -14
  10. data/app/views/super/application/_collection_header.html.erb +15 -0
  11. data/app/views/super/application/_filter.html.erb +14 -0
  12. data/app/views/super/application/_filter_type_select.html.erb +31 -0
  13. data/app/views/super/application/_filter_type_text.html.erb +22 -0
  14. data/app/views/super/application/_filter_type_timestamp.html.erb +35 -0
  15. data/app/views/super/application/_flash.html.erb +13 -13
  16. data/app/views/super/application/_form_field__destroy.html.erb +7 -3
  17. data/app/views/super/application/_form_field_select.html.erb +14 -8
  18. data/app/views/super/application/_form_field_text.html.erb +13 -5
  19. data/app/views/super/application/_form_fieldset.html.erb +1 -1
  20. data/app/views/super/application/_form_has_many.html.erb +5 -5
  21. data/app/views/super/application/_form_inline_errors.html.erb +1 -1
  22. data/app/views/super/application/_member_header.html.erb +16 -0
  23. data/app/views/super/application/_super_layout.html.erb +29 -0
  24. data/app/views/super/application/_super_pagination.html.erb +16 -0
  25. data/app/views/super/application/_super_panel.html.erb +7 -0
  26. data/app/views/super/application/_super_schema_display_actions.html.erb +5 -0
  27. data/app/views/super/application/_super_schema_display_index.html.erb +24 -0
  28. data/app/views/super/application/_super_schema_display_show.html.erb +8 -0
  29. data/app/views/super/application/_super_schema_form.html.erb +15 -0
  30. data/app/views/super/application/edit.html.erb +1 -6
  31. data/app/views/super/application/index.html.erb +1 -1
  32. data/app/views/super/application/new.html.erb +1 -6
  33. data/app/views/super/application/show.html.erb +1 -1
  34. data/app/views/super/feather/{_chevron_down.svg → _chevron_down.html} +0 -0
  35. data/config/locales/en.yml +5 -0
  36. data/docs/README.md +6 -0
  37. data/docs/cheat.md +41 -0
  38. data/docs/faq.md +44 -0
  39. data/docs/quick_start.md +45 -0
  40. data/docs/webpacker.md +17 -0
  41. data/docs/yard_customizations.rb +41 -0
  42. data/frontend/super-frontend/build.js +12 -12
  43. data/frontend/super-frontend/dist/application.css +93514 -27381
  44. data/frontend/super-frontend/dist/application.js +1186 -11151
  45. data/frontend/super-frontend/package.json +8 -4
  46. data/frontend/super-frontend/postcss.config.js +4 -4
  47. data/frontend/super-frontend/src/javascripts/super/application.ts +11 -9
  48. data/frontend/super-frontend/src/javascripts/super/apply_template_controller.ts +19 -0
  49. data/frontend/super-frontend/src/javascripts/super/rails__ujs.d.ts +1 -1
  50. data/frontend/super-frontend/src/javascripts/super/toggle_pending_destruction_controller.ts +15 -0
  51. data/frontend/super-frontend/src/stylesheets/super/application.css +63 -0
  52. data/frontend/super-frontend/tailwind.config.js +12 -4
  53. data/frontend/super-frontend/yarn.lock +1429 -1372
  54. data/lib/generators/super/install/install_generator.rb +16 -0
  55. data/lib/generators/super/resource/templates/resources_controller.rb.tt +1 -31
  56. data/lib/generators/super/webpacker/webpacker_generator.rb +10 -1
  57. data/lib/super.rb +26 -5
  58. data/lib/super/action_inquirer.rb +2 -2
  59. data/lib/super/assets.rb +108 -38
  60. data/lib/super/client_error.rb +43 -0
  61. data/lib/super/compatibility.rb +25 -0
  62. data/lib/super/configuration.rb +21 -69
  63. data/lib/super/controls.rb +9 -37
  64. data/lib/super/controls/optional.rb +79 -0
  65. data/lib/super/controls/required.rb +13 -0
  66. data/lib/super/controls/steps.rb +114 -0
  67. data/lib/super/display.rb +66 -3
  68. data/lib/super/display/guesser.rb +34 -0
  69. data/lib/super/display/schema_types.rb +55 -25
  70. data/lib/super/engine.rb +7 -1
  71. data/lib/super/error.rb +9 -9
  72. data/lib/super/filter.rb +12 -0
  73. data/lib/super/filter/form_object.rb +97 -0
  74. data/lib/super/filter/guesser.rb +30 -0
  75. data/lib/super/filter/operator.rb +103 -0
  76. data/lib/super/filter/plugin.rb +47 -0
  77. data/lib/super/filter/schema_types.rb +112 -0
  78. data/lib/super/form.rb +35 -0
  79. data/lib/super/form/builder.rb +48 -0
  80. data/lib/super/form/guesser.rb +27 -0
  81. data/lib/super/form/schema_types.rb +20 -23
  82. data/lib/super/form/strong_params.rb +29 -0
  83. data/lib/super/layout.rb +47 -0
  84. data/lib/super/link.rb +110 -0
  85. data/lib/super/pagination.rb +74 -8
  86. data/lib/super/panel.rb +30 -0
  87. data/lib/super/partial.rb +23 -0
  88. data/lib/super/partial/resolving.rb +24 -0
  89. data/lib/super/plugin.rb +34 -63
  90. data/lib/super/schema.rb +12 -22
  91. data/lib/super/schema/common.rb +25 -0
  92. data/lib/super/schema/guesser.rb +77 -0
  93. data/lib/super/version.rb +1 -1
  94. data/lib/super/view_helper.rb +43 -0
  95. metadata +133 -33
  96. data/app/views/super/application/_form.html.erb +0 -14
  97. data/app/views/super/application/_index.html.erb +0 -60
  98. data/app/views/super/application/_show.html.erb +0 -12
  99. data/frontend/super-frontend/src/javascripts/super/nested_attributes_controller.ts +0 -33
  100. data/lib/super/inline_callback.rb +0 -82
  101. data/lib/super/test_support/copy_app_templates/controllers/favorite_things_controller.rb +0 -50
  102. data/lib/super/test_support/copy_app_templates/controllers/members_controller.rb +0 -57
  103. data/lib/super/test_support/copy_app_templates/controllers/ships_controller.rb +0 -47
  104. data/lib/super/test_support/copy_app_templates/migrations/20190216224956_create_members.rb +0 -11
  105. data/lib/super/test_support/copy_app_templates/migrations/20190803143320_create_ships.rb +0 -11
  106. data/lib/super/test_support/copy_app_templates/migrations/20190806014121_add_ship_to_members.rb +0 -5
  107. data/lib/super/test_support/copy_app_templates/migrations/20191126050453_create_favorite_things.rb +0 -10
  108. data/lib/super/test_support/copy_app_templates/models/favorite_thing.rb +0 -7
  109. data/lib/super/test_support/copy_app_templates/models/member.rb +0 -23
  110. data/lib/super/test_support/copy_app_templates/models/ship.rb +0 -3
  111. data/lib/super/test_support/copy_app_templates/routes.rb +0 -11
  112. data/lib/super/test_support/copy_app_templates/seeds.rb +0 -2
  113. data/lib/super/test_support/fixtures/favorite_things.yml +0 -9
  114. data/lib/super/test_support/fixtures/members.yml +0 -336
  115. data/lib/super/test_support/fixtures/ships.yml +0 -10
  116. data/lib/super/test_support/generate_copy_app.rb +0 -41
  117. data/lib/super/test_support/generate_dummy.rb +0 -93
  118. data/lib/super/test_support/starfleet_seeder.rb +0 -50
  119. data/lib/super/view.rb +0 -25
  120. data/lib/tasks/super_tasks.rake +0 -4
@@ -29,6 +29,22 @@ module Super
29
29
  create_file("app/controllers/#{controller_namespace}/.keep", "")
30
30
  end
31
31
 
32
+ def copy_cheatsheet
33
+ super_path = Pathname.new(Gem.loaded_specs["super"].full_gem_path).expand_path
34
+ super_cheat_path = super_path.join("docs", "cheat.md")
35
+
36
+ path =
37
+ if options["controller_namespace"].present?
38
+ "app/controllers/#{controller_namespace}/README.md"
39
+ else
40
+ "app/controllers/README.md"
41
+ end
42
+
43
+ create_file(path) do
44
+ super_cheat_path.read.sub(%r{<!--.*?-->}m, "").strip + "\n"
45
+ end
46
+ end
47
+
32
48
  def setup_sprockets4_manifest
33
49
  append_to_file "app/assets/config/manifest.js", "//= link super_manifest.js\n"
34
50
  end
@@ -6,40 +6,10 @@ class <%= class_name.pluralize %>Controller < <%= parent_controller_name %>
6
6
  Controls.new
7
7
  end
8
8
 
9
- class Controls
10
- def title
11
- <%= class_name %>.name.pluralize
12
- end
13
-
9
+ class Controls < Super::Controls
14
10
  def model
15
11
  <%= class_name %>
16
12
  end
17
-
18
- def scope(action:)
19
- if action.read?
20
- <%= class_name %>.all
21
- else
22
- <%= class_name %>.all
23
- end
24
- end
25
-
26
- def permitted_params(params, action:)
27
- if action.create?
28
- params.require(:<%= file_name %>).permit()
29
- else
30
- params.require(:<%= file_name %>).permit()
31
- end
32
- end
33
-
34
- def display_schema(action:)
35
- Super::Schema.new(Super::Display::SchemaTypes.new) do |fields, type|
36
- end
37
- end
38
-
39
- def form_schema(action:)
40
- Super::Schema.new(Super::Form::SchemaTypes.new) do |fields, type|
41
- end
42
- end
43
13
  end
44
14
  end
45
15
  <% end -%>
@@ -9,9 +9,18 @@ module Super
9
9
  )
10
10
  end
11
11
 
12
+ def set_asset_handler_to_webpacker
13
+ insert_into_file(
14
+ "config/initializers/super.rb",
15
+ " c.javascripts = Super::Assets.use_webpacker(c.javascripts)\n" \
16
+ " c.stylesheets = Super::Assets.use_webpacker(c.stylesheets)\n",
17
+ before: /\bend\b/
18
+ )
19
+ end
20
+
12
21
  def remind_about_erb
13
22
  say "Make sure ERB is set up for Webpacker!", :bold
14
- say "Run if needed: rails webpacker:install:erb", :bold
23
+ say "Run if needed: bundle exec rails webpacker:install:erb"
15
24
  end
16
25
  end
17
26
  end
@@ -1,19 +1,40 @@
1
1
  require "tsort"
2
- require "active_support/concern"
2
+
3
+ require "rails/engine"
4
+
5
+ require "super/schema/common"
3
6
 
4
7
  require "super/action_inquirer"
5
8
  require "super/assets"
9
+ require "super/client_error"
10
+ require "super/compatibility"
6
11
  require "super/configuration"
7
12
  require "super/controls"
8
- require "super/engine"
9
- require "super/error"
10
13
  require "super/display"
14
+ require "super/display/guesser"
11
15
  require "super/display/schema_types"
16
+ require "super/error"
17
+ require "super/filter"
18
+ require "super/filter/form_object"
19
+ require "super/filter/guesser"
20
+ require "super/filter/operator"
21
+ require "super/filter/plugin"
22
+ require "super/filter/schema_types"
23
+ require "super/form"
24
+ require "super/form/builder"
25
+ require "super/form/guesser"
12
26
  require "super/form/schema_types"
13
- require "super/inline_callback"
27
+ require "super/form/strong_params"
28
+ require "super/layout"
29
+ require "super/link"
14
30
  require "super/navigation/automatic"
15
31
  require "super/pagination"
32
+ require "super/panel"
33
+ require "super/partial"
16
34
  require "super/plugin"
17
35
  require "super/schema"
36
+ require "super/schema/guesser"
18
37
  require "super/version"
19
- require "super/view"
38
+ require "super/view_helper"
39
+
40
+ require "super/engine"
@@ -1,7 +1,7 @@
1
1
  module Super
2
2
  # ```ruby
3
3
  # action = Super::ActionInquirer.new(
4
- # Super::ActionInquirer.default_resources,
4
+ # Super::ActionInquirer.default_for_resources,
5
5
  # :index
6
6
  # )
7
7
  #
@@ -15,7 +15,7 @@ module Super
15
15
 
16
16
  # @return [Hash<Symbol, Array<Symbol>>] default settings for initialization
17
17
  #
18
- def self.default_resources
18
+ def self.default_for_resources
19
19
  {
20
20
  read: %i[index show new edit],
21
21
  write: %i[create update destroy],
@@ -1,8 +1,64 @@
1
1
  module Super
2
2
  # Utilities for determining whether to use Sprockets or Webpacker
3
3
  class Assets
4
- def self.sprockets_available?
5
- Gem::Dependency.new("sprockets").matching_specs.any?
4
+ def self.webpacker(path, **arguments)
5
+ Asset.new(handler: Handler.webpacker, path: path, arguments: arguments)
6
+ end
7
+
8
+ def self.sprockets(path, **arguments)
9
+ Asset.new(handler: Handler.sprockets, path: path, arguments: arguments)
10
+ end
11
+
12
+ def self.auto(path, **arguments)
13
+ Asset.new(handler: Handler.auto, path: path, arguments: arguments)
14
+ end
15
+
16
+ def self.use_webpacker(assets, grep: nil)
17
+ assets = [assets] if !assets.kind_of?(Array)
18
+
19
+ assets.map do |asset|
20
+ grep_matches = grep && asset === grep
21
+ if grep_matches || !grep
22
+ asset.instance_variable_set(:@handler, Handler.webpacker)
23
+ end
24
+
25
+ asset
26
+ end
27
+ end
28
+
29
+ def self.use_sprockets(assets, grep: nil)
30
+ assets = [assets] if !assets.kind_of?(Array)
31
+
32
+ assets.map do |asset|
33
+ grep_matches = grep && asset === grep
34
+ if grep_matches || !grep
35
+ asset.instance_variable_set(:@asset_handler, Handler.sprockets)
36
+ end
37
+
38
+ asset
39
+ end
40
+ end
41
+
42
+ class Asset
43
+ def initialize(handler:, path:, arguments:)
44
+ @handler = handler
45
+ @path = path
46
+ @arguments = arguments
47
+ end
48
+
49
+ attr_reader :handler
50
+ attr_reader :path
51
+ attr_reader :arguments
52
+
53
+ def ===(other)
54
+ return true if path == other
55
+ return true if other.is_a?(Regexp) && path.match?(other)
56
+ return true if handler == other
57
+ return true if handler.to_sym == other
58
+ return true if handler.to_s == other
59
+
60
+ false
61
+ end
6
62
  end
7
63
 
8
64
  def self.dist(gem_name, package_name)
@@ -15,49 +71,63 @@ module Super
15
71
  gem_path.join("frontend", package_name, "dist")
16
72
  end
17
73
 
18
- def self.auto
19
- @auto ||=
20
- if Gem::Dependency.new("sprockets", "~> 4.0").matching_specs.any?
21
- sprockets
22
- elsif Gem::Dependency.new("sprockets", "~> 3.0").matching_specs.any?
23
- sprockets
24
- elsif Gem::Dependency.new("sprockets", "~> 2.0").matching_specs.any?
25
- sprockets
26
- elsif Gem::Dependency.new("webpacker", "~> 4.0").matching_specs.any?
27
- webpacker
28
- elsif Gem::Dependency.new("webpacker", "~> 3.0").matching_specs.any?
29
- webpacker
30
- else
31
- none
32
- end
33
- end
74
+ class Handler
75
+ def self.auto
76
+ @auto ||=
77
+ if Gem::Dependency.new("sprockets", "~> 4.0").matching_specs.any?
78
+ sprockets
79
+ elsif Gem::Dependency.new("sprockets", "~> 3.0").matching_specs.any?
80
+ sprockets
81
+ elsif Gem::Dependency.new("sprockets", "~> 2.0").matching_specs.any?
82
+ sprockets
83
+ elsif Gem::Dependency.new("webpacker", "~> 4.0").matching_specs.any?
84
+ webpacker
85
+ elsif Gem::Dependency.new("webpacker", "~> 3.0").matching_specs.any?
86
+ webpacker
87
+ else
88
+ none
89
+ end
90
+ end
34
91
 
35
- def self.sprockets
36
- new(:sprockets)
37
- end
92
+ def self.sprockets_available?
93
+ Gem::Dependency.new("sprockets").matching_specs.any? && defined?(Sprockets)
94
+ end
38
95
 
39
- def self.webpacker
40
- new(:webpacker)
41
- end
96
+ def self.sprockets
97
+ @sprockets ||= new(:sprockets)
98
+ end
42
99
 
43
- def self.none
44
- new(:none)
45
- end
100
+ def self.webpacker
101
+ @webpacker ||= new(:webpacker)
102
+ end
46
103
 
47
- def initialize(asset_handler)
48
- @asset_handler = asset_handler
49
- end
104
+ def self.none
105
+ @none ||= new(:none)
106
+ end
50
107
 
51
- def sprockets?
52
- @asset_handler == :sprockets
53
- end
108
+ def initialize(asset_handler)
109
+ @asset_handler = asset_handler
110
+ end
54
111
 
55
- def webpacker?
56
- @asset_handler == :webpacker
57
- end
112
+ def sprockets?
113
+ @asset_handler == :sprockets
114
+ end
115
+
116
+ def webpacker?
117
+ @asset_handler == :webpacker
118
+ end
119
+
120
+ def none?
121
+ @asset_handler == :none
122
+ end
123
+
124
+ def to_sym
125
+ @asset_handler
126
+ end
58
127
 
59
- def none?
60
- @asset_handler == :none
128
+ def to_s
129
+ @asset_handler.to_s
130
+ end
61
131
  end
62
132
  end
63
133
  end
@@ -0,0 +1,43 @@
1
+ module Super
2
+ # A container class for all user-facing (4xx) errors thrown by this library.
3
+ #
4
+ # See also `Super::Error`
5
+ class ClientError < StandardError
6
+ class BadRequest < ClientError; end
7
+ class Unauthorized < ClientError; end
8
+ class Forbidden < ClientError; end
9
+ class NotFound < ClientError; end
10
+ class UnprocessableEntity < ClientError; end
11
+
12
+ module Handling
13
+ extend ActiveSupport::Concern
14
+
15
+ included do
16
+ rescue_from ::Super::ClientError do |exception|
17
+ code, default_message =
18
+ case exception
19
+ when ClientError::UnprocessableEntity
20
+ [422, "Unprocessable entity"]
21
+ when ClientError::NotFound
22
+ [404, "Not found"]
23
+ when ClientError::Forbidden
24
+ [403, "Forbidden"]
25
+ when ClientError::Unauthorized
26
+ [401, "Unauthorized"]
27
+ when ClientError::BadRequest, ClientError
28
+ [400, "Bad request"]
29
+ end
30
+
31
+ flash.now.alert =
32
+ if exception.message == exception.class.name.to_s
33
+ default_message
34
+ else
35
+ exception.message
36
+ end
37
+
38
+ render "nothing", status: code
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,25 @@
1
+ module Super
2
+ module Compatability
3
+ module_function
4
+
5
+ # Rails 5.1 and after lets you find field errors using either a string or a
6
+ # symbol.
7
+ def errable_fields(field)
8
+ if Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR == 0
9
+ [field.to_s, field.to_sym]
10
+ else
11
+ field
12
+ end
13
+ end
14
+
15
+ def sanitize_sql_like(query)
16
+ if ActiveRecord::VERSION::MAJOR == 4
17
+ ActiveRecord::Base.send(:sanitize_sql_like, query)
18
+ elsif ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR <= 1
19
+ ActiveRecord::Base.send(:sanitize_sql_like, query)
20
+ else
21
+ ActiveRecord::Base.sanitize_sql_like(query)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -19,81 +19,33 @@ module Super
19
19
  # end
20
20
  # ```
21
21
  class Configuration
22
- module ConfigurationLogic # @api private
23
- def self.included(base)
24
- base.extend(ClassMethods)
25
- end
22
+ def initialize
23
+ self.title = "Super Admin"
24
+ self.index_records_per_page = 20
25
+ self.controller_namespace = "admin"
26
+ self.route_namespace = :admin
26
27
 
27
- def initialize
28
- self.class.defaults.each do |key, value|
29
- if value.respond_to?(:call)
30
- value = value.call
31
- end
28
+ controller_plugins.use(prepend: Super::Filter::ControllerMethods)
29
+ controller_plugins.use(prepend: Super::Pagination::ControllerMethods)
32
30
 
33
- public_send("#{key}=", value)
34
- end
35
- end
36
-
37
- def configured?(attr)
38
- instance_variable_defined?("@#{attr}")
39
- end
40
-
41
- module ClassMethods
42
- def defaults
43
- @defaults ||= {}
44
- end
45
-
46
- def wraps
47
- @wraps ||= {}
48
- end
49
-
50
- def configure(attr, wrap: nil, enum: nil, **kwargs)
51
- if kwargs.key?(:default)
52
- defaults[attr] = kwargs[:default]
53
- end
54
-
55
- define_method(attr) do
56
- if !configured?(attr)
57
- raise Error::UnconfiguredConfiguration, "unconfigured: #{attr}"
58
- end
59
-
60
- result = instance_variable_get("@#{attr}")
61
-
62
- if wrap.nil?
63
- result
64
- else
65
- wrap.call(result)
66
- end
67
- end
68
-
69
- define_method("#{attr}=") do |value|
70
- if enum.is_a?(Array)
71
- if !enum.include?(value)
72
- raise Error::InvalidConfiguration,
73
- "tried to set `#{attr}` to `#{value.inspect}`, " \
74
- "expected: #{enum.join(", ")}"
75
- end
76
- end
31
+ self.javascripts = [Super::Assets.auto("super/application")]
32
+ self.stylesheets = [Super::Assets.auto("super/application")]
33
+ end
77
34
 
78
- instance_variable_set("@#{attr}", value)
79
- value
80
- end
81
- end
82
- end
35
+ attr_accessor :title
36
+ attr_accessor :index_records_per_page
37
+ attr_accessor :controller_namespace
38
+ attr_writer :route_namespace
39
+ def route_namespace
40
+ [@route_namespace].flatten
83
41
  end
84
42
 
85
- include ConfigurationLogic
43
+ attr_accessor :javascripts
44
+ attr_accessor :stylesheets
86
45
 
87
- # @!attribute [rw]
88
- configure :title
89
- # @!attribute [rw]
90
- configure :index_resources_per_page, default: 20
91
- # @!attribute [rw]
92
- configure :controller_namespace, default: "admin"
93
- # @!attribute [rw]
94
- configure :route_namespace, default: :admin, wrap: -> (val) { [val].flatten }
95
- # @!attribute [rw]
96
- configure :asset_handler, default: -> { Super::Assets.auto }
46
+ def controller_plugins
47
+ Plugin::Registry.controller
48
+ end
97
49
 
98
50
  # @api private
99
51
  def path_parts(*parts)