phlexi-table 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/lib/phlexi/table/base.rb +356 -74
  3. data/lib/phlexi/table/components/actions_column.rb +43 -0
  4. data/lib/phlexi/table/components/base.rb +22 -18
  5. data/lib/phlexi/table/components/column.rb +14 -0
  6. data/lib/phlexi/table/components/column_group.rb +23 -0
  7. data/lib/phlexi/table/components/concerns/displays_data.rb +27 -0
  8. data/lib/phlexi/table/components/concerns/displays_header.rb +30 -0
  9. data/lib/phlexi/table/components/concerns/groups_columns.rb +36 -0
  10. data/lib/phlexi/table/components/data_column.rb +33 -0
  11. data/lib/phlexi/table/components/header_cell.rb +19 -0
  12. data/lib/phlexi/table/components/options/alignment.rb +31 -0
  13. data/lib/phlexi/table/components/options/labels.rb +30 -0
  14. data/lib/phlexi/table/components/selection_cell.rb +17 -0
  15. data/lib/phlexi/table/components/selection_column.rb +21 -0
  16. data/lib/phlexi/table/components/sortable_header_cell.rb +74 -0
  17. data/lib/phlexi/table/display_theme.rb +6 -0
  18. data/lib/phlexi/table/html.rb +15 -0
  19. data/lib/phlexi/table/options/captions.rb +22 -0
  20. data/lib/phlexi/table/{field_options/description.rb → options/descriptions.rb} +5 -5
  21. data/lib/phlexi/table/theme.rb +25 -0
  22. data/lib/phlexi/table/version.rb +1 -1
  23. data/lib/phlexi/table/wrapped_object.rb +27 -0
  24. data/lib/phlexi/table.rb +5 -6
  25. metadata +49 -25
  26. data/lib/phlexi/table/components/concerns/displays_value.rb +0 -54
  27. data/lib/phlexi/table/components/date_time.rb +0 -49
  28. data/lib/phlexi/table/components/description.rb +0 -21
  29. data/lib/phlexi/table/components/hint.rb +0 -21
  30. data/lib/phlexi/table/components/label.rb +0 -15
  31. data/lib/phlexi/table/components/number.rb +0 -37
  32. data/lib/phlexi/table/components/placeholder.rb +0 -15
  33. data/lib/phlexi/table/components/string.rb +0 -17
  34. data/lib/phlexi/table/components/wrapper.rb +0 -17
  35. data/lib/phlexi/table/field_options/associations.rb +0 -21
  36. data/lib/phlexi/table/field_options/attachments.rb +0 -21
  37. data/lib/phlexi/table/field_options/hints.rb +0 -22
  38. data/lib/phlexi/table/field_options/inferred_types.rb +0 -129
  39. data/lib/phlexi/table/field_options/labels.rb +0 -28
  40. data/lib/phlexi/table/field_options/placeholders.rb +0 -18
  41. data/lib/phlexi/table/field_options/themes.rb +0 -132
  42. data/lib/phlexi/table/structure/dom.rb +0 -42
  43. data/lib/phlexi/table/structure/field_builder.rb +0 -158
  44. data/lib/phlexi/table/structure/field_collection.rb +0 -39
  45. data/lib/phlexi/table/structure/namespace.rb +0 -123
  46. data/lib/phlexi/table/structure/namespace_collection.rb +0 -40
  47. data/lib/phlexi/table/structure/node.rb +0 -24
@@ -1,39 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Phlexi
4
- module Table
5
- module Structure
6
- class FieldCollection
7
- include Enumerable
8
-
9
- class Builder
10
- attr_reader :key, :index
11
-
12
- def initialize(key, field, index)
13
- @key = key.to_s
14
- @field = field
15
- @index = index
16
- end
17
-
18
- def field(**)
19
- @field.class.new(key, **, parent: @field).tap do |field|
20
- yield field if block_given?
21
- end
22
- end
23
- end
24
-
25
- def initialize(field:, collection:, &)
26
- @field = field
27
- @collection = collection
28
- each(&) if block_given?
29
- end
30
-
31
- def each(&)
32
- @collection.each.with_index do |item, index|
33
- yield Builder.new(item, @field, index)
34
- end
35
- end
36
- end
37
- end
38
- end
39
- end
@@ -1,123 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Phlexi
4
- module Table
5
- module Structure
6
- # A Namespace maps an object to values, but doesn't actually have a value itself. For
7
- # example, a `User` object or ActiveRecord model could be passed into the `:user` namespace.
8
- #
9
- # To access single values on a Namespace, #field can be used.
10
- #
11
- # To access nested objects within a namespace, two methods are available:
12
- #
13
- # 1. #nest_one: Used for single nested objects, such as if a `User belongs_to :profile` in
14
- # ActiveRecord. This method returns another Namespace object.
15
- #
16
- # 2. #nest_many: Used for collections of nested objects, such as if a `User has_many :addresses` in
17
- # ActiveRecord. This method returns a NamespaceCollection object.
18
- class Namespace < Structure::Node
19
- include Enumerable
20
-
21
- attr_reader :builder_klass, :object
22
-
23
- def initialize(key, parent:, builder_klass:, object: nil)
24
- super(key, parent: parent)
25
- @builder_klass = builder_klass
26
- @object = object
27
- @children = {}
28
- yield self if block_given?
29
- end
30
-
31
- def field(key, **attributes)
32
- create_child(key, attributes.delete(:builder_klass) || builder_klass, object: object, **attributes).tap do |field|
33
- yield field if block_given?
34
- end
35
- end
36
-
37
- # Creates a `Namespace` child instance with the parent set to the current instance, adds to
38
- # the `@children` Hash to ensure duplicate child namespaces aren't created, then calls the
39
- # method on the `@object` to get the child object to pass into that namespace.
40
- #
41
- # For example, if a `User#permission` returns a `Permission` object, we could map that to a
42
- # display like this:
43
- #
44
- # ```ruby
45
- # Phlexi::Table(user) do |display|
46
- # display.nest_one :profile do |profile|
47
- # render profile.field(:gender).text
48
- # end
49
- # end
50
- # ```
51
- def nest_one(key, object: nil, &)
52
- object ||= object_value_for(key: key)
53
- create_child(key, self.class, object:, builder_klass:, &)
54
- end
55
-
56
- # Wraps an array of objects in Namespace classes. For example, if `User#addresses` returns
57
- # an enumerable or array of `Address` classes:
58
- #
59
- # ```ruby
60
- # Phlexi::Table(user) do |display|
61
- # render display.field(:email).text
62
- # render display.field(:name).text
63
- # display.nest_many :addresses do |address|
64
- # render address.field(:street).text
65
- # render address.field(:state).text
66
- # render address.field(:zip).text
67
- # end
68
- # end
69
- # ```
70
- # The object within the block is a `Namespace` object that maps each object within the enumerable
71
- # to another `Namespace` or `Field`.
72
- def nest_many(key, collection: nil, &)
73
- collection ||= Array(object_value_for(key: key))
74
- create_child(key, NamespaceCollection, collection:, &)
75
- end
76
-
77
- # Iterates through the children of the current namespace, which could be `Namespace` or `Field`
78
- # objects.
79
- def each(&)
80
- @children.values.each(&)
81
- end
82
-
83
- def dom_id
84
- @dom_id ||= begin
85
- id = if object.nil?
86
- nil
87
- elsif object.class.respond_to?(:primary_key)
88
- object.public_send(object.class.primary_key) || :new
89
- elsif object.respond_to?(:id)
90
- object.id || :new
91
- end
92
- [key, id].compact.join("_").underscore
93
- end
94
- end
95
-
96
- # Creates a root Namespace
97
- def self.root(*, builder_klass:, **, &)
98
- new(*, parent: nil, builder_klass:, **, &)
99
- end
100
-
101
- protected
102
-
103
- # Calls the corresponding method on the object for the `key` name, if it exists. For example
104
- # if the `key` is `email` on `User`, this method would call `User#email` if the method is
105
- # present.
106
- #
107
- # This method could be overwritten if the mapping between the `@object` and `key` name is not
108
- # a method call. For example, a `Hash` would be accessed via `user[:email]` instead of `user.send(:email)`
109
- def object_value_for(key:)
110
- @object.send(key) if @object.respond_to? key
111
- end
112
-
113
- private
114
-
115
- # Checks if the child exists. If it does then it returns that. If it doesn't, it will
116
- # build the child.
117
- def create_child(key, child_class, **kwargs, &block)
118
- @children.fetch(key) { @children[key] = child_class.new(key, parent: self, **kwargs, &block) }
119
- end
120
- end
121
- end
122
- end
123
- end
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Phlexi
4
- module Table
5
- module Structure
6
- class NamespaceCollection < Node
7
- include Enumerable
8
-
9
- def initialize(key, parent:, collection: nil, &block)
10
- raise ArgumentError, "block is required" unless block.present?
11
-
12
- super(key, parent: parent)
13
-
14
- @collection = collection
15
- @block = block
16
- each(&block)
17
- end
18
-
19
- private
20
-
21
- def each(&)
22
- namespaces.each(&)
23
- end
24
-
25
- # Builds and memoizes namespaces for the collection.
26
- #
27
- # @return [Array<Namespace>] An array of namespace objects.
28
- def namespaces
29
- @namespaces ||= @collection.map.with_index do |object, key|
30
- build_namespace(key, object: object)
31
- end
32
- end
33
-
34
- def build_namespace(index, **)
35
- parent.class.new(index, parent: self, builder_klass: parent.builder_klass, **)
36
- end
37
- end
38
- end
39
- end
40
- end
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Phlexi
4
- module Table
5
- module Structure
6
- # Superclass for Namespace and Field classes. Represents a node in the display tree structure.
7
- #
8
- # @attr_reader [Symbol] key The node's key
9
- # @attr_reader [Node, nil] parent The node's parent in the tree structure
10
- class Node
11
- attr_reader :key, :parent
12
-
13
- # Initializes a new Node instance.
14
- #
15
- # @param key [Symbol, String] The key for the node
16
- # @param parent [Node, nil] The parent node
17
- def initialize(key, parent:)
18
- @key = :"#{key}"
19
- @parent = parent
20
- end
21
- end
22
- end
23
- end
24
- end