bord 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +193 -0
  4. data/Rakefile +9 -0
  5. data/app/assets/javascripts/bord/application.js +2361 -0
  6. data/app/assets/javascripts/bord/attract.js +319 -0
  7. data/app/assets/javascripts/bord/attractive.js +1642 -0
  8. data/app/assets/javascripts/bord/entry.js +67 -0
  9. data/app/assets/javascripts/bord/request.js +314 -0
  10. data/app/assets/javascripts/bord/runtime/addons/response.js +51 -0
  11. data/app/assets/javascripts/bord/runtime/addons/template.js +120 -0
  12. data/app/assets/javascripts/bord/runtime/debug.js +18 -0
  13. data/app/assets/stylesheets/bord/application.css +736 -0
  14. data/app/controllers/bord/actions_controller.rb +29 -0
  15. data/app/controllers/bord/application_controller.rb +23 -0
  16. data/app/controllers/bord/pages_controller.rb +13 -0
  17. data/app/controllers/bord/records_controller.rb +14 -0
  18. data/app/controllers/bord/widgets_controller.rb +18 -0
  19. data/app/helpers/bord/application_helper.rb +4 -0
  20. data/app/jobs/bord/application_job.rb +4 -0
  21. data/app/models/bord/action.rb +8 -0
  22. data/app/models/bord/action_parameters.rb +19 -0
  23. data/app/models/bord/allowed_actions.rb +65 -0
  24. data/app/models/bord/application_record.rb +5 -0
  25. data/app/models/bord/attract_actions.rb +101 -0
  26. data/app/models/bord/category.rb +18 -0
  27. data/app/models/bord/dashboard.rb +25 -0
  28. data/app/models/bord/entry.rb +98 -0
  29. data/app/models/bord/page/decorator.rb +33 -0
  30. data/app/models/bord/page/record_decorator.rb +44 -0
  31. data/app/models/bord/page.rb +82 -0
  32. data/app/models/bord/widget/decorator.rb +69 -0
  33. data/app/models/bord/widget.rb +40 -0
  34. data/app/models/concerns/bord/conditionable.rb +8 -0
  35. data/app/views/bord/pages/show.html.erb +9 -0
  36. data/app/views/bord/records/show.html.erb +13 -0
  37. data/app/views/bord/shared/_templates.html.erb +75 -0
  38. data/app/views/bord/widgets/_action.html.erb +7 -0
  39. data/app/views/bord/widgets/_widget.html.erb +25 -0
  40. data/app/views/bord/widgets/index.html.erb +17 -0
  41. data/app/views/layouts/bord/application.html.erb +38 -0
  42. data/config/routes.rb +11 -0
  43. data/lib/bord/configuration.rb +60 -0
  44. data/lib/bord/engine.rb +11 -0
  45. data/lib/bord/loader.rb +8 -0
  46. data/lib/bord/options.rb +17 -0
  47. data/lib/bord/presets/action_mailbox.json +20 -0
  48. data/lib/bord/presets/active_storage.json +20 -0
  49. data/lib/bord/presets/devise.json +1 -0
  50. data/lib/bord/presets/good_job.json +25 -0
  51. data/lib/bord/presets/sidekiq.json +28 -0
  52. data/lib/bord/presets/solid_queue.json +12 -0
  53. data/lib/bord/presets/stripe.json +30 -0
  54. data/lib/bord/presets/user.json +22 -0
  55. data/lib/bord/version.rb +3 -0
  56. data/lib/bord.rb +71 -0
  57. data/lib/generators/bord/install_generator.rb +16 -0
  58. data/lib/generators/bord/presets_generator.rb +62 -0
  59. metadata +116 -0
@@ -0,0 +1,29 @@
1
+ module Bord
2
+ class ActionsController < ApplicationController
3
+ before_action :set_page, :set_widget, only: :create
4
+
5
+ def create
6
+ AllowedActions.validate!(@page || @widget, controller: self, params: params)
7
+
8
+ Bord::Action.run(params)
9
+
10
+ if request.headers["Attract"].present?
11
+ render json: {actions: AttractActions.response_actions(@widget, @page, view_context)}
12
+ else
13
+ flash[:notice] = "Action completed"
14
+ redirect_back fallback_location: root_path
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def set_page
21
+ owner = params[:owner].to_s
22
+ @page = Page.find!(owner.delete_prefix("page:")) if owner.start_with?("page:")
23
+ end
24
+
25
+ def set_widget
26
+ @widget = Bord.widgets.find { it.id == params[:owner].to_s } unless @page
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,23 @@
1
+ module Bord
2
+ class ApplicationController < Bord.parent_controller.constantize
3
+ before_action :require_configured_controller
4
+
5
+ class << self
6
+ attr_accessor :unconfigured_warning_logged
7
+ end
8
+ self.unconfigured_warning_logged = false
9
+
10
+ private
11
+
12
+ def require_configured_controller
13
+ return unless Bord.locked?
14
+
15
+ unless self.class.unconfigured_warning_logged
16
+ self.class.unconfigured_warning_logged = true
17
+ Rails.logger.warn("Bord is mounted but not configured for production. All /bord requests return 404. Set an authenticated parent controller, for example: Bord.configure { |config| config.parent_controller = \"Admin::BaseController\" }")
18
+ end
19
+
20
+ raise ActiveRecord::RecordNotFound
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ module Bord
2
+ class PagesController < ApplicationController
3
+ def show
4
+ @page = Page.find!(params[:id])
5
+ @page.context = self
6
+
7
+ respond_to do |format|
8
+ format.html
9
+ format.json { render json: {actions: @page.decorate(view_context: view_context).actions} }
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ module Bord
2
+ class RecordsController < ApplicationController
3
+ def show
4
+ @page = Page.find!(params[:page_id])
5
+ @page.context = self
6
+ @record = @page.find_record(params[:id])
7
+
8
+ respond_to do |format|
9
+ format.html
10
+ format.json { render json: {actions: @page.decorate_record(@record, view_context: view_context).actions} }
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ module Bord
2
+ class WidgetsController < ApplicationController
3
+ before_action :set_dashboard
4
+
5
+ def index
6
+ end
7
+
8
+ def show
9
+ render json: {actions: @dashboard.widgets.flat_map { it.decorate(view_context: view_context).actions }}
10
+ end
11
+
12
+ private
13
+
14
+ def set_dashboard
15
+ @dashboard = Dashboard.new(self)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,4 @@
1
+ module Bord
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Bord
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,8 @@
1
+ module Bord
2
+ class Action
3
+ def self.run(params)
4
+ record = GlobalID::Locator.locate(params[:record]) or raise ActiveRecord::RecordNotFound
5
+ record.public_send(params[:method], **ActionParameters.from(params))
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,19 @@
1
+ require "json"
2
+
3
+ module Bord
4
+ module ActionParameters
5
+ module_function
6
+
7
+ def from(params)
8
+ flattened = params
9
+ .except(:controller, :action, :authenticity_token, :method, :record, :owner, :params, :format)
10
+ .permit!
11
+ .to_h
12
+ .symbolize_keys
13
+
14
+ nested = params[:params]
15
+ nested = JSON.parse(nested) if nested.is_a?(String)
16
+ flattened.merge((nested || {}).symbolize_keys)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,65 @@
1
+ module Bord
2
+ class AllowedActions
3
+ def self.validate!(owner, controller:, params:)
4
+ new(owner, controller).validate!(params)
5
+ end
6
+
7
+ def initialize(owner, controller)
8
+ @owner = owner
9
+ @controller = controller
10
+ end
11
+
12
+ def validate!(params)
13
+ requested = {
14
+ record: params[:record].to_s,
15
+ method: params[:method].to_s,
16
+ parameters: normalized(ActionParameters.from(params))
17
+ }
18
+
19
+ return if requests.include?(requested)
20
+
21
+ Rails.logger.warn("Bord action rejected (owner: #{params[:owner].to_s.inspect}, method: #{params[:method].to_s.inspect}, record: #{params[:record].to_s.inspect}): no configured action match")
22
+ raise ActiveRecord::RecordNotFound
23
+ end
24
+
25
+ private
26
+
27
+ def requests
28
+ definitions.filter_map do |definition|
29
+ next unless definition[:record] && definition[:method]
30
+
31
+ {
32
+ record: definition[:record].to_s,
33
+ method: definition[:method].to_s,
34
+ parameters: normalized(definition.except(:record, :method, :confirm, :variant))
35
+ }
36
+ end
37
+ end
38
+
39
+ def definitions
40
+ definitions =
41
+ if @owner.is_a?(Page)
42
+ @owner.context = @controller
43
+ @owner.records.flat_map { |record| Array(record.actions) }
44
+ elsif @owner&.kind&.list?
45
+ @owner.items(@controller.view_context).to_a.flat_map { |item| Entry.build(item, view_context: @controller.view_context).actions }
46
+ elsif @owner
47
+ @owner.actions
48
+ else
49
+ []
50
+ end
51
+
52
+ definitions.filter_map do |action|
53
+ next unless action.is_a?(Hash)
54
+ definition = action.symbolize_keys[:action]
55
+ next unless definition.is_a?(Hash)
56
+
57
+ definition.symbolize_keys
58
+ end
59
+ end
60
+
61
+ def normalized(hash)
62
+ hash.map { |key, value| [key.to_s, value.to_s] }.sort
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,5 @@
1
+ module Bord
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,101 @@
1
+ module Bord
2
+ module AttractActions
3
+ module_function
4
+
5
+ def build(actions, target_selector:, owner:)
6
+ Array(actions).flat_map do |action|
7
+ action = action.symbolize_keys
8
+ href = action[:href]
9
+ definition = action[:action]
10
+
11
+ if definition
12
+ definition_actions(action, definition.symbolize_keys, target_selector, owner)
13
+ elsif href
14
+ link_actions(action, href, target_selector)
15
+ else
16
+ []
17
+ end
18
+ end
19
+ end
20
+
21
+ def response_actions(widget, page, view_context)
22
+ return page.decorate.actions if page
23
+ return [flash("Action completed")] unless widget
24
+
25
+ if widget.kind.list?
26
+ [
27
+ {action: "remove", targets: "#widget-#{widget.id}-entries > *"},
28
+ *widget.decorate(view_context: view_context).actions
29
+ ]
30
+ else
31
+ [flash("Action completed"), *widget.decorate(view_context: view_context).actions]
32
+ end
33
+ end
34
+
35
+ def flash(message)
36
+ {
37
+ action: "replace",
38
+ target: "bord-flash",
39
+ template: "bord-flash-template",
40
+ data: {message: message}
41
+ }
42
+ end
43
+
44
+ def link_actions(action, href, target_selector)
45
+ label = action[:label]
46
+ variant = action[:variant]
47
+ attributes = {href: href}
48
+ attributes[:variant] = variant if variant
49
+
50
+ [
51
+ {
52
+ action: "append",
53
+ targets: target_selector,
54
+ template: "bord-entry-link",
55
+ data: {label: label}
56
+ },
57
+ {
58
+ action: "setAttributes",
59
+ targets: "#{target_selector} > :last-child",
60
+ data: attributes
61
+ }
62
+ ]
63
+ end
64
+
65
+ def definition_actions(action, definition, target_selector, owner)
66
+ label = action[:label]
67
+ record = definition[:record]
68
+ method = definition[:method]
69
+ confirm = action[:confirm] || definition[:confirm]
70
+ variant = action[:variant]
71
+ parameters = definition.except(:record, :method, :confirm, :variant)
72
+
73
+ [
74
+ {
75
+ action: "append",
76
+ targets: target_selector,
77
+ template: "bord-entry-action",
78
+ data: {
79
+ label: label,
80
+ owner: owner,
81
+ record: record,
82
+ method: method,
83
+ params: parameters.to_json
84
+ }
85
+ }
86
+ ].tap do |attract_actions|
87
+ attributes = {}
88
+ attributes[:confirm] = confirm if confirm
89
+ attributes[:variant] = variant if variant
90
+
91
+ if attributes.any?
92
+ attract_actions << {
93
+ action: "setAttributes",
94
+ targets: "#{target_selector} > :last-child",
95
+ data: attributes
96
+ }
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,18 @@
1
+ module Bord
2
+ class Category
3
+ include Conditionable
4
+
5
+ attr_reader :id, :label, :widgets, :position
6
+
7
+ def initialize(id:, label: nil, position: nil, **options)
8
+ @id = id.to_s
9
+ @label = label || id.to_s.humanize
10
+ @widgets = []
11
+ @position = position && Options.position(position, "category :#{id}")
12
+
13
+ @if_condition = options.delete(:if)
14
+ @unless_condition = options.delete(:unless)
15
+ Options.assert_known!(options, context: "category :#{id}")
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,25 @@
1
+ module Bord
2
+ class Dashboard
3
+ def initialize(view)
4
+ @view = view
5
+ end
6
+
7
+ def categories
8
+ Bord.sort_by_position(Bord.categories.select { it.show?(@view) }).map { |category| [category, category_widgets(category)] }
9
+ end
10
+
11
+ def uncategoryed_widgets
12
+ Bord.sort_by_position(Bord.uncategoryed_widgets.select { it.show?(@view) })
13
+ end
14
+
15
+ def widgets
16
+ Bord.widgets.select { it.show?(@view) }
17
+ end
18
+
19
+ private
20
+
21
+ def category_widgets(category)
22
+ Bord.sort_by_position(category.widgets.select { it.show?(@view) })
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,98 @@
1
+ module Bord
2
+ class Entry
3
+ attr_reader :id, :title, :description, :href, :icon, :actions
4
+
5
+ def initialize(id: nil, title: nil, description: nil, href: nil, icon: nil, actions: [])
6
+ @id = id
7
+ @title = title
8
+ @description = description
9
+ @href = href
10
+ @icon = icon
11
+ @actions = actions
12
+ end
13
+
14
+ class << self
15
+ def build(record, view_context: nil, page: nil)
16
+ return from_hash(record) if record.is_a?(Hash)
17
+ return from_page_record(record) if record.is_a?(Page::Record)
18
+
19
+ attributes = infer(record, view_context, page)
20
+
21
+ if record.respond_to?(:to_bord)
22
+ override = record.to_bord.symbolize_keys
23
+ override[:title] ||= override.delete(:label)
24
+ attributes.merge!(override)
25
+ end
26
+
27
+ from_hash(attributes)
28
+ end
29
+
30
+ def from_hash(attributes)
31
+ attributes = attributes.symbolize_keys
32
+
33
+ new(
34
+ id: attributes[:id],
35
+ title: attributes[:title] || attributes[:label],
36
+ description: attributes[:description] || attributes[:subtitle],
37
+ href: attributes[:href],
38
+ icon: attributes[:icon],
39
+ actions: attributes[:actions] || []
40
+ )
41
+ end
42
+
43
+ def from_page_record(record)
44
+ new(
45
+ title: record.title,
46
+ description: record.description,
47
+ icon: record.icon,
48
+ actions: record.actions
49
+ )
50
+ end
51
+
52
+ def infer(record, view_context, page)
53
+ {
54
+ id: identifier(record),
55
+ title: default_title(record),
56
+ description: nil,
57
+ href: href(record, view_context, page),
58
+ icon: icon(record),
59
+ actions: []
60
+ }
61
+ end
62
+
63
+ def default_title(record)
64
+ return record.to_s unless record.respond_to?(:model_name)
65
+
66
+ identifier = identifier(record)
67
+
68
+ identifier ? "#{record.model_name.human} ##{identifier}" : record.model_name.human
69
+ end
70
+
71
+ def identifier(record)
72
+ return record.to_gid.to_s if record.respond_to?(:to_gid)
73
+
74
+ record.to_param if record.respond_to?(:to_param)
75
+ rescue
76
+ nil
77
+ end
78
+
79
+ def href(record, view_context, page)
80
+ return unless view_context
81
+
82
+ if page
83
+ view_context.bord.page_record_path(page.id, record.to_param)
84
+ elsif view_context.respond_to?(:main_app)
85
+ view_context.main_app.polymorphic_path(record)
86
+ end
87
+ rescue
88
+ nil
89
+ end
90
+
91
+ def icon(record)
92
+ return unless record.respond_to?(:model_name)
93
+
94
+ record.model_name.singular
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,33 @@
1
+ module Bord
2
+ class Page
3
+ class Decorator < SimpleDelegator
4
+ def initialize(page, view_context: nil)
5
+ super(page)
6
+ @view_context = view_context
7
+ end
8
+
9
+ def actions
10
+ [{action: "remove", targets: "#page-entries > *"}].tap do |attract_actions|
11
+ records.each do |record|
12
+ entry = Entry.build(record, view_context: @view_context, page: self)
13
+
14
+ attract_actions << {
15
+ action: "append",
16
+ target: "page-entries",
17
+ template: "bord-page-entry",
18
+ data: {title: entry.title, description: entry.description}
19
+ }
20
+
21
+ attract_actions.concat(
22
+ AttractActions.build(
23
+ entry.actions,
24
+ target_selector: "#page-entries > :last-child .actions",
25
+ owner: "page:#{id}"
26
+ )
27
+ )
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,44 @@
1
+ module Bord
2
+ class Page
3
+ class RecordDecorator < SimpleDelegator
4
+ def initialize(record, page:, view_context: nil)
5
+ super(record)
6
+
7
+ @page = page
8
+ @view_context = view_context
9
+ end
10
+
11
+ def actions
12
+ entry = Entry.build(__getobj__, view_context: @view_context, page: @page)
13
+ fields = __getobj__.is_a?(Hash) ? Array(__getobj__[:fields]) : []
14
+
15
+ [
16
+ {action: "remove", targets: "#page-detail > *"},
17
+ {
18
+ action: "append",
19
+ target: "page-detail",
20
+ template: "bord-page-detail",
21
+ data: {title: entry.title, description: entry.description}
22
+ }
23
+ ].tap do |attract_actions|
24
+ fields.each do |field|
25
+ attract_actions << {
26
+ action: "append",
27
+ target: "#page-detail .details",
28
+ template: "bord-page-field",
29
+ data: {label: field[:label], value: field[:value]}
30
+ }
31
+ end
32
+
33
+ attract_actions.concat(
34
+ AttractActions.build(
35
+ entry.actions,
36
+ target_selector: "#page-detail .actions",
37
+ owner: "page:#{@page.id}"
38
+ )
39
+ )
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,82 @@
1
+ module Bord
2
+ class Page
3
+ Record = Data.define(:icon, :title, :description, :actions) do
4
+ def initialize(title:, icon: nil, description: nil, actions: [])
5
+ super
6
+ end
7
+ end
8
+
9
+ attr_reader :id, :label, :description
10
+ attr_accessor :context
11
+
12
+ def initialize(id:, records:, label: nil, description: nil, record: nil, **options)
13
+ @id = id.to_s
14
+ @label = label || id.to_s.humanize
15
+ @description = description
16
+ @records_lambda = records
17
+ @record_lambda = record
18
+
19
+ Options.assert_known!(options, context: "page :#{id}")
20
+ end
21
+
22
+ def records
23
+ result = @records_lambda
24
+ result = evaluate_lambda(result) if result.respond_to?(:call)
25
+
26
+ (result || []).map do |entry|
27
+ next entry unless entry.is_a?(Hash)
28
+
29
+ entry = entry.symbolize_keys
30
+
31
+ assert_known_record_keys!(entry)
32
+ build_record(entry)
33
+ end
34
+ end
35
+
36
+ def find_record(id)
37
+ raise ActiveRecord::RecordNotFound unless @record_lambda
38
+
39
+ record = @record_lambda
40
+ record = context ? context.instance_exec(id, &record) : record.call(id) if record.respond_to?(:call)
41
+
42
+ record or raise ActiveRecord::RecordNotFound
43
+ end
44
+
45
+ def decorate(view_context: nil)
46
+ Page::Decorator.new(self, view_context: view_context)
47
+ end
48
+
49
+ def decorate_record(record, view_context: nil)
50
+ Page::RecordDecorator.new(record, page: self, view_context: view_context)
51
+ end
52
+
53
+ private
54
+
55
+ def evaluate_lambda(callable)
56
+ context ? context.instance_exec(&callable) : callable.call
57
+ end
58
+
59
+ def assert_known_record_keys!(entry)
60
+ unknown = entry.keys - Record.members
61
+ return if unknown.empty?
62
+
63
+ raise ArgumentError, "Unknown key#{"s" if unknown.size > 1} #{unknown.map(&:inspect).join(", ")} in records for page :#{id} (allowed: #{Record.members.map(&:inspect).join(", ")})"
64
+ end
65
+
66
+ def build_record(entry)
67
+ Record.new(**entry)
68
+ rescue ArgumentError => error
69
+ raise ArgumentError, "#{error.message} (page :#{id})"
70
+ end
71
+
72
+ class << self
73
+ def all
74
+ Bord.pages
75
+ end
76
+
77
+ def find!(id)
78
+ all.find { it.id == id.to_s } or raise ActiveRecord::RecordNotFound
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,69 @@
1
+ module Bord
2
+ class Widget
3
+ class Decorator < SimpleDelegator
4
+ def initialize(widget, view_context: nil)
5
+ super(widget)
6
+
7
+ @view_context = view_context
8
+ end
9
+
10
+ def actions
11
+ return value_actions unless kind.list?
12
+
13
+ items(@view_context).to_a.flat_map { entry_actions(it) }
14
+ end
15
+
16
+ private
17
+
18
+ def value_actions
19
+ [
20
+ {
21
+ action: "replace",
22
+ target: "widget-#{id}-value",
23
+ template: href ? "bord-value-link" : "bord-value",
24
+ data: {value: value(@view_context)}
25
+ }
26
+ ].tap do |attract_actions|
27
+ if href
28
+ attract_actions << {
29
+ action: "setAttributes",
30
+ targets: "#widget-#{id} .value",
31
+ data: {href: href}
32
+ }
33
+ end
34
+ end
35
+ end
36
+
37
+ def entry_actions(item)
38
+ entry = Entry.build(item, view_context: @view_context)
39
+
40
+ [
41
+ {
42
+ action: "append",
43
+ target: "widget-#{id}-entries",
44
+ template: "bord-entry",
45
+ data: {label: entry.title}
46
+ }
47
+ ].tap do |attract_actions|
48
+ attributes = {href: entry.href}.compact
49
+
50
+ if attributes.any?
51
+ attract_actions << {
52
+ action: "setAttributes",
53
+ targets: "#widget-#{id}-entries > :last-child",
54
+ data: attributes
55
+ }
56
+ end
57
+
58
+ attract_actions.concat(
59
+ AttractActions.build(
60
+ entry.actions,
61
+ target_selector: "#widget-#{id}-entries > :last-child .actions",
62
+ owner: id
63
+ )
64
+ )
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end