super 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +0 -8
  3. data/README.md +4 -8
  4. data/app/controllers/super/application_controller.rb +5 -5
  5. data/app/views/layouts/super/application.html.erb +1 -3
  6. data/app/views/super/application/{_super_schema_display_actions.html.erb → _display_actions.html.erb} +0 -0
  7. data/app/views/super/application/{_super_schema_display_index.html.erb → _display_index.html.erb} +3 -3
  8. data/app/views/super/application/_display_show.html.erb +8 -0
  9. data/app/views/super/application/{_super_schema_form.html.erb → _form.html.erb} +2 -2
  10. data/app/views/super/application/{_super_layout.html.erb → _layout.html.erb} +7 -7
  11. data/app/views/super/application/{_super_pagination.html.erb → _pagination.html.erb} +1 -1
  12. data/app/views/super/application/{_super_panel.html.erb → _panel.html.erb} +2 -2
  13. data/lib/generators/super/install/install_generator.rb +16 -7
  14. data/lib/generators/super/install/templates/base_controller.rb.tt +9 -1
  15. data/lib/generators/super/install/templates/initializer.rb.tt +9 -2
  16. data/lib/generators/super/resource/resource_generator.rb +105 -30
  17. data/lib/generators/super/resource/templates/resources_controller.rb.tt +3 -9
  18. data/lib/super.rb +2 -0
  19. data/lib/super/configuration.rb +14 -23
  20. data/lib/super/controls.rb +7 -2
  21. data/lib/super/controls/optional.rb +1 -0
  22. data/lib/super/display.rb +16 -8
  23. data/lib/super/display/schema_types.rb +70 -33
  24. data/lib/super/error.rb +7 -0
  25. data/lib/super/form.rb +1 -1
  26. data/lib/super/layout.rb +1 -1
  27. data/lib/super/link.rb +5 -0
  28. data/lib/super/navigation/automatic.rb +2 -2
  29. data/lib/super/pagination.rb +1 -1
  30. data/lib/super/panel.rb +1 -1
  31. data/lib/super/useful/builder.rb +25 -0
  32. data/lib/super/useful/enum.rb +63 -0
  33. data/lib/super/version.rb +1 -1
  34. metadata +17 -19
  35. data/STABILITY.md +0 -50
  36. data/app/views/super/application/_super_schema_display_show.html.erb +0 -8
  37. data/docs/README.md +0 -8
  38. data/docs/action_text.md +0 -48
  39. data/docs/faq.md +0 -44
  40. data/docs/installation.md +0 -21
  41. data/docs/quick_start.md +0 -30
  42. data/docs/webpacker.md +0 -25
  43. data/docs/yard_customizations.rb +0 -43
  44. data/lib/super/controls/required.rb +0 -15
data/lib/super.rb CHANGED
@@ -5,6 +5,8 @@ require "tsort"
5
5
  require "rails/engine"
6
6
 
7
7
  require "super/schema/common"
8
+ require "super/useful/builder"
9
+ require "super/useful/enum"
8
10
 
9
11
  require "super/action_inquirer"
10
12
  require "super/assets"
@@ -21,36 +21,27 @@ module Super
21
21
  # end
22
22
  # ```
23
23
  class Configuration
24
- def initialize
25
- self.title = "Super Admin"
26
- self.index_records_per_page = 20
27
- self.controller_namespace = "admin"
28
- self.route_namespace = :admin
29
-
30
- controller_plugins.use(prepend: Super::Pagination::ControllerMethods)
24
+ include ActiveSupport::Configurable
31
25
 
32
- self.javascripts = [Super::Assets.auto("super/application")]
33
- self.stylesheets = [Super::Assets.auto("super/application")]
26
+ config_accessor(:title) { "Super Admin" }
27
+ config_accessor(:index_records_per_page) { 100 }
28
+ config_accessor(:javascripts) do
29
+ [Super::Assets.auto("super/application")]
34
30
  end
35
-
36
- attr_accessor :title
37
- attr_accessor :index_records_per_page
38
- attr_accessor :controller_namespace
39
- attr_writer :route_namespace
40
- def route_namespace
41
- [@route_namespace].flatten
31
+ config_accessor(:stylesheets) do
32
+ [Super::Assets.auto("super/application")]
42
33
  end
43
34
 
44
- attr_accessor :javascripts
45
- attr_accessor :stylesheets
35
+ config_accessor(:path) { "/admin" }
36
+ config_accessor(:generator_module) { "admin" }
37
+ config_accessor(:generator_as) { "admin" }
46
38
 
47
- def controller_plugins
48
- Plugin::Registry.controller
39
+ def initialize
40
+ controller_plugins.use(prepend: Super::Pagination::ControllerMethods)
49
41
  end
50
42
 
51
- # @api private
52
- def path_parts(*parts)
53
- route_namespace + parts
43
+ def controller_plugins
44
+ Plugin::Registry.controller
54
45
  end
55
46
  end
56
47
  end
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "super/controls/optional"
4
- require "super/controls/required"
5
4
  require "super/controls/steps"
6
5
  require "super/controls/view"
7
6
 
@@ -9,9 +8,15 @@ module Super
9
8
  # The base Controls class. Most parts of Super can be configured by
10
9
  # customizing its methods.
11
10
  class Controls
12
- include Required
13
11
  include Optional
14
12
  include Steps
15
13
  include View
14
+
15
+ # Specifies the model. This is a required method
16
+ #
17
+ # @return [ActiveRecord::Base]
18
+ def model
19
+ raise NotImplementedError
20
+ end
16
21
  end
17
22
  end
@@ -98,6 +98,7 @@ module Super
98
98
  )
99
99
  attribute_names =
100
100
  display_schema(action: action).each_attribute.map do |key, val|
101
+ val = val.build if val.respond_to?(:build)
101
102
  key if val.real?
102
103
  end
103
104
 
data/lib/super/display.rb CHANGED
@@ -45,25 +45,33 @@ module Super
45
45
  raise Super::Error::Initalization,
46
46
  "You must call the `#apply` method after instantiating Super::Display"
47
47
  elsif @action_inquirer.index?
48
- "super_schema_display_index"
48
+ "display_index"
49
49
  elsif @action_inquirer.show?
50
- "super_schema_display_show"
50
+ "display_show"
51
51
  else
52
- "super_schema_display_#{@action_inquirer.action}"
52
+ "display_#{@action_inquirer.action}"
53
53
  end
54
54
  end
55
55
 
56
56
  # @private
57
- def render_field(template:, record:, column:)
57
+ def render_attribute(template:, record:, column:)
58
58
  formatter = @fields[column]
59
+ formatter = formatter.build if formatter.respond_to?(:build)
59
60
 
60
61
  formatted =
61
- if formatter.real?
62
+ SchemaTypes::TYPES
63
+ .case(formatter.type)
64
+ .when(:record) do
65
+ formatter.present(column, record)
66
+ end
67
+ .when(:column) do
62
68
  value = record.public_send(column)
63
- formatter.present(value)
64
- else
65
- formatter.present
69
+ formatter.present(column, value)
70
+ end
71
+ .when(:none) do
72
+ formatter.present(column)
66
73
  end
74
+ .result
67
75
 
68
76
  if formatted.respond_to?(:to_partial_path)
69
77
  if formatted.respond_to?(:locals)
@@ -3,35 +3,61 @@
3
3
  module Super
4
4
  class Display
5
5
  class SchemaTypes
6
- class Dynamic
7
- def initialize(ignore_nil: true, &transform_block)
8
- @transform_block = transform_block
9
- @ignore_nil = ignore_nil
10
- end
6
+ TYPES = Useful::Enum.new(:column, :record, :none)
11
7
 
12
- def present(value)
13
- return nil if value.nil? && @ignore_nil
8
+ class Builder
9
+ extend Useful::Builder
14
10
 
15
- @transform_block.call(value)
11
+ builder_with_block def transform(&block)
12
+ @transform_block = block
16
13
  end
17
14
 
18
- def real?
19
- true
15
+ builder def real; @real = true; end
16
+ builder def computed; @real = false; end
17
+
18
+ builder def column; @type = :column; end
19
+ builder def record; @type = :record; end
20
+ builder def none; @type = :none; end
21
+
22
+ builder def ignore_nil; @ignore_nil = true; end
23
+
24
+ def build
25
+ Built.new(
26
+ real: @real,
27
+ type: @type,
28
+ ignore_nil: !!@ignore_nil,
29
+ &@transform_block
30
+ )
20
31
  end
21
32
  end
22
33
 
23
- class Bypass
24
- def initialize(partial:, real:)
25
- @partial = partial
34
+ class Built
35
+ def initialize(real:, type:, ignore_nil:, &transform_block)
26
36
  @real = real
37
+ @type = type
38
+ @ignore_nil = ignore_nil
39
+ @transform_block = transform_block
27
40
  end
28
41
 
29
- def present
30
- Partial.new(@partial)
31
- end
42
+ def real?; @real; end
43
+ attr_reader :type
44
+
45
+ def present(attribute_name, value = nil)
46
+ if @transform_block.nil?
47
+ if attribute_name
48
+ raise Error::ArgumentError, "Transformation block is not set for column: #{attribute_name}"
49
+ else
50
+ raise Error::ArgumentError, "Transformation block is not set!"
51
+ end
52
+ end
53
+
54
+ return nil if value.nil? && @ignore_nil
32
55
 
33
- def real?
34
- @real
56
+ if @type == :none
57
+ @transform_block.call
58
+ else
59
+ @transform_block.call(value)
60
+ end
35
61
  end
36
62
  end
37
63
 
@@ -40,33 +66,44 @@ module Super
40
66
  @fields = fields
41
67
  end
42
68
 
43
- def string
44
- Dynamic.new(&:to_s)
69
+ def real(type = :column, &transform_block)
70
+ TYPES
71
+ .case(type)
72
+ .when(:column) { Builder.new.real.ignore_nil.column.transform(&transform_block) }
73
+ .when(:record) { Builder.new.real.ignore_nil.record.transform(&transform_block) }
74
+ .when(:none) { Builder.new.real.ignore_nil.none.transform(&transform_block) }
75
+ .result
45
76
  end
46
77
 
47
- alias text string
78
+ def computed(type = :column, &transform_block)
79
+ TYPES
80
+ .case(type)
81
+ .when(:column) { Builder.new.computed.ignore_nil.column.transform(&transform_block) }
82
+ .when(:record) { Builder.new.computed.ignore_nil.record.transform(&transform_block) }
83
+ .when(:none) { Builder.new.computed.ignore_nil.none.transform(&transform_block) }
84
+ .result
85
+ end
48
86
 
49
- def timestamp
50
- Dynamic.new(&:iso8601)
87
+ def manual(&transform_block)
88
+ real(:column, &transform_block)
51
89
  end
52
90
 
91
+ def string; real(&:to_s); end
92
+ alias text string
93
+
94
+ def timestamp; real(&:to_s); end
95
+
53
96
  def rich_text
54
- Dynamic.new do |value|
97
+ real do |value|
55
98
  Partial.new("display_rich_text", locals: { rich_text: value })
56
99
  end
57
100
  end
58
101
 
59
- def manual(&transform_block)
60
- Dynamic.new(&transform_block)
61
- end
62
-
63
- def dynamic(&transform_block)
64
- Dynamic.new(&transform_block)
65
- end
66
-
67
102
  def actions
68
103
  @actions_called = true
69
- Bypass.new(partial: "super_schema_display_actions", real: false)
104
+ Builder.new.computed.none.transform do
105
+ Partial.new("display_actions")
106
+ end
70
107
  end
71
108
 
72
109
  # @private
data/lib/super/error.rb CHANGED
@@ -27,5 +27,12 @@ module Super
27
27
  # Error raised when something wasn't initalized correctly, and if there isn't
28
28
  # a more specific error
29
29
  class Initalization < Error; end
30
+ class ArgumentError < Error; end
31
+
32
+ class Enum < Error
33
+ class ImpossibleValue < Enum; end
34
+ class ArgumentError < Enum; end
35
+ class UndefinedCase < Enum; end
36
+ end
30
37
  end
31
38
  end
data/lib/super/form.rb CHANGED
@@ -31,7 +31,7 @@ module Super
31
31
  end
32
32
 
33
33
  def to_partial_path
34
- "super_schema_form"
34
+ "form"
35
35
  end
36
36
  end
37
37
  end
data/lib/super/layout.rb CHANGED
@@ -19,7 +19,7 @@ module Super
19
19
  attr_reader :footers
20
20
 
21
21
  def to_partial_path
22
- "super_layout"
22
+ "layout"
23
23
  end
24
24
 
25
25
  def resolve(template)
data/lib/super/link.rb CHANGED
@@ -80,6 +80,11 @@ module Super
80
80
  }
81
81
  end
82
82
 
83
+ def self.polymorphic_parts(*parts_tail)
84
+ parts_head = Super.configuration.path.strip.gsub(%r{\A/+}, "").gsub(%r{/+\z}, "").strip.split("/")
85
+ parts_head + parts_tail
86
+ end
87
+
83
88
  def initialize(text, href, **options)
84
89
  @text = text
85
90
  @href = href
@@ -5,8 +5,8 @@ module Super
5
5
  # Traverses the defined Rails Routes and attempts to build a list of links.
6
6
  # This is used for building the nav bar on each admin page.
7
7
  class Automatic
8
- def initialize(route_namespace:)
9
- route_namespace = route_namespace.to_s
8
+ def initialize(route_namespace: Super.configuration.path)
9
+ route_namespace = route_namespace.strip.gsub(%r{\A/+}, "").gsub(%r{/+\z}, "").strip
10
10
 
11
11
  if route_namespace.include?("/")
12
12
  raise "Can't be nested namespace"
@@ -55,7 +55,7 @@ module Super
55
55
  end
56
56
 
57
57
  def to_partial_path
58
- "super_pagination"
58
+ "pagination"
59
59
  end
60
60
 
61
61
  private
data/lib/super/panel.rb CHANGED
@@ -26,7 +26,7 @@ module Super
26
26
  end
27
27
 
28
28
  def to_partial_path
29
- "super_panel"
29
+ "panel"
30
30
  end
31
31
  end
32
32
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Super
4
+ module Useful
5
+ module Builder
6
+ def builder(method_name)
7
+ alias_method("original_#{method_name}", method_name)
8
+
9
+ define_method(method_name) do
10
+ send("original_#{method_name}")
11
+ self
12
+ end
13
+ end
14
+
15
+ def builder_with_block(method_name)
16
+ alias_method("original_#{method_name}", method_name)
17
+
18
+ define_method(method_name) do |&block|
19
+ send("original_#{method_name}", &block)
20
+ self
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Super
4
+ module Useful
5
+ class Enum
6
+ def initialize(*choices)
7
+ @choices = choices.flatten.map { |choice| [choice, nil] }.to_h
8
+ end
9
+
10
+ def case(chosen)
11
+ Case.new(@choices, chosen)
12
+ end
13
+
14
+ class Case
15
+ def initialize(choices, chosen)
16
+ if !choices.key?(chosen)
17
+ raise Error::Enum::ImpossibleValue,
18
+ "`#{chosen}` isn't in: #{choices.keys.map(&:inspect).join(", ")}"
19
+ end
20
+
21
+ @choices = choices
22
+ @chosen = chosen
23
+ @whens = {}
24
+ end
25
+
26
+ def when(*keys, &block)
27
+ if !block_given?
28
+ raise Error::ArgumentError, "must receive block"
29
+ end
30
+
31
+ keys.each do |key|
32
+ if !@choices.key?(key)
33
+ raise Error::Enum::ImpossibleValue, "`#{key.inspect}` is not a possibility"
34
+ end
35
+
36
+ @whens[key] = block
37
+ end
38
+
39
+ self
40
+ end
41
+
42
+ def result
43
+ if @choices.size != @whens.size
44
+ missing_keys = @choices.keys - @whens.keys
45
+ raise Error::Enum::UndefinedCase, "Unset cases: #{missing_keys.join(", ")}"
46
+ end
47
+
48
+ @whens[@chosen].call(@value)
49
+ end
50
+
51
+ private
52
+
53
+ def matching_possibilities_and_checks?
54
+ if @whens.size == @choices.size
55
+ return true
56
+ end
57
+
58
+ false
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end