yiffspace-tables 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bebb3ea3cfa83ecf7855b951bd615710b0687fb45a1e43a7283b78ff64470755
4
+ data.tar.gz: 5b6bc96aacd6950716b12b090dc17f42b42a186344305f2fa2d7bc183de84e04
5
+ SHA512:
6
+ metadata.gz: bf23e91575a639f2f189707270e817feee48cf4a118872f9cc22fdf1f173c69a9a3d5844633c650c06fcadeb39b12e59356cb7396c133a4098f9f0c0ab338760
7
+ data.tar.gz: 0a2f003542e6cc24a7162796d4292c3cb2a4145f7c4b9871a05aac47e7e07edcd58ab5cb48474fd0a675794b64ef4c3459ac05780f36f2c42303c7e3fa11d43d
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## 0.0.1
2
+
3
+ - Initial release, extracted from the `yiffspace` gem's `YiffSpace::Tables::Builder`.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright Donovan_DMC
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # YiffSpace::Tables
2
+
3
+ HTML table-building DSL (`YiffSpace::Tables::Builder`), for
4
+ https://yiff.space and related projects.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem "yiffspace-tables"
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ ```bash
17
+ $ bundle install
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ ```ruby
23
+ YiffSpace::Tables::Builder.new(@tags) do |table|
24
+ table.column(:name)
25
+ table.column(:post_count)
26
+ end
27
+ ```
28
+
29
+ See `YiffSpace::Tables::Builder` for the full API.
30
+
31
+ ## Contributing
32
+
33
+ Go away
34
+
35
+ ## License
36
+
37
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require("bundler/gem_tasks")
4
+
5
+ # CI releases from an already-tagged, already-pushed commit checked out at a detached HEAD, so
6
+ # the default release task's git tag/push step has nothing to do and fails trying to push a
7
+ # branch ref that does not exist in that checkout.
8
+ Rake::Task["release"].clear
9
+ task("release" => "release:rubygem_push")
@@ -0,0 +1,169 @@
1
+ # frozen_string_literal: true
2
+
3
+ module YiffSpace
4
+ module Tables
5
+ # A helper class for building HTML tables. Used in views.
6
+ #
7
+ # @example
8
+ # <%= table_for @tags do |table| %>
9
+ # <% table.column :name do |tag| %>
10
+ # <%= link_to_wiki "?", tag.name %>
11
+ # <%= link_to tag.name, posts_path(tags: tag.name) %>
12
+ # <% end %>
13
+ # <% table.column :post_count %>
14
+ # <% end %>
15
+ #
16
+ # @see app/views/table_builder/_table.html.erb
17
+ class Builder
18
+ # Represents a single column in the table.
19
+ class Column
20
+ attr_reader(:attribute, :name, :block, :header_attributes, :body_attributes, :caption)
21
+
22
+ # Define a table column.
23
+ #
24
+ # @example
25
+ # <% table.column :post_count %>
26
+ #
27
+ # @example
28
+ # <% table.column :name do |tag| %>
29
+ # <%= tag.pretty_name %>
30
+ # <% end %>
31
+ #
32
+ # @param attribute [Symbol] The attribute in the model the column is for.
33
+ # The column's name and value will come from this attribute by default.
34
+ # @param name [String] the column's name, if different from the attribute name.
35
+ # @param column [String] the column name
36
+ # @param th [Hash] the HTML attributes for the column's <th> tag.
37
+ # @param td [Hash] the HTML attributes for the column's <td> tag.
38
+ # @param width [String] the HTML width value for the <th> tag.
39
+ # @yieldparam item a block that returns the column value based on the item.
40
+ def initialize(attribute = nil, column: nil, th: {}, td: {}, width: nil, name: nil, &block)
41
+ @attribute = attribute
42
+ @column = column
43
+ @header_attributes = { width: width, **th }
44
+ @body_attributes = td
45
+ @block = block
46
+
47
+ @name = name || attribute
48
+ @name = @name.to_s.titleize unless @name.is_a?(String)
49
+
50
+ return unless @name.present? || @column.present?
51
+
52
+ if @column.present?
53
+ column_class = "#{@column}-column"
54
+ else
55
+ column_class = "#{@name.parameterize.dasherize}-column"
56
+ end
57
+ @header_attributes[:class] = "#{column_class} #{@header_attributes[:class]}".strip
58
+ @body_attributes[:class] = "#{column_class} #{@body_attributes[:class]}".strip
59
+ end
60
+
61
+ # Returns the value of the table cell.
62
+ # @param item [ApplicationRecord] the table cell item
63
+ # @param row [Integer] the table row number
64
+ # @param column [Integer] the table column number
65
+ # @return [#to_s] the value of the table cell
66
+ def value(item, row, column)
67
+ if block.present?
68
+ view = block.binding.receiver
69
+ if view.respond_to?(:capture)
70
+ view.capture { block.call(item, row, column, self) }
71
+ else
72
+ block.call(item, row, column, self)
73
+ end
74
+ elsif attribute.is_a?(Symbol)
75
+ item.send(attribute)
76
+ else
77
+ ""
78
+ end
79
+ end
80
+ end
81
+
82
+ # Represents a custom row in the table body.
83
+ class Row
84
+ attr_reader(:block, :row_attributes)
85
+
86
+ def initialize(row_attributes = {}, &block)
87
+ @row_attributes = row_attributes
88
+ @block = block
89
+ end
90
+
91
+ def custom?
92
+ true
93
+ end
94
+
95
+ def value(index)
96
+ view = block.binding.receiver
97
+ if view.respond_to?(:capture)
98
+ view.capture { block.call(index, self) }
99
+ else
100
+ block.call(index, self)
101
+ end
102
+ end
103
+ end
104
+
105
+ attr_reader(:columns, :table_attributes, :row_attributes, :items)
106
+
107
+ # Build a table for an array of objects, one object per row.
108
+ #
109
+ # The <table> tag is automatically given an HTML id of the form `{name}-table`.
110
+ # For example, `posts-table`, `tags-table`.
111
+ #
112
+ # The <tr> tag is automatically given an HTML id of the form `{name}-{id}`.
113
+ # For example, `post-1234`, `tag-4567`, etc. Each <tr> tag also gets a set of
114
+ # data attributes for each model; see #html_data_attributes in app/policies.
115
+ #
116
+ # @param items [Array<ApplicationRecord>] The list of ActiveRecord objects to
117
+ # build the table for. One item per table row.
118
+ # @param tr [Hash] optional HTML attributes for the <tr> tag for each row
119
+ # @param table_attributes [Hash] optional HTML attributes for the <table> tag
120
+ # @yieldparam table [self] the table being built
121
+ def initialize(items, tr: {}, **table_attributes)
122
+ @items = items
123
+ @columns = []
124
+ @table_attributes = { class: "striped", **table_attributes }
125
+ @row_attributes = tr
126
+
127
+ @table_attributes[:id] ||= "#{items.model_name.plural.dasherize}-table" if items.respond_to?(:model_name)
128
+
129
+ yield(self) if block_given?
130
+ end
131
+
132
+ # Set the table caption
133
+ def caption
134
+ @caption = yield if block_given?
135
+ @caption
136
+ end
137
+
138
+ # Add a column to the table.
139
+ # @example
140
+ # table.column(:name)
141
+ def column(*, **options, &)
142
+ opt = options.extract!(:if, :unless)
143
+ return if (opt.key?(:if) && !opt[:if]) || (opt.key?(:unless) && opt[:unless])
144
+
145
+ @columns << Column.new(*, **options, &)
146
+ end
147
+
148
+ def row(row_attributes = {}, &)
149
+ @items = @items.to_a unless @items.is_a?(Array)
150
+ @items << Row.new(row_attributes, &)
151
+ end
152
+
153
+ # Return the HTML attributes for each <tr> tag.
154
+ # @param item [ApplicationRecord] the item for this row
155
+ # @param _row [Integer] the row number (unused)
156
+ # @return [Hash] the <tr> attributes
157
+ def all_row_attributes(item, _row)
158
+ return item.row_attributes if item.is_a?(Row)
159
+ return {} unless item.is_a?(YiffSpace.config.application_record_class)
160
+
161
+ {
162
+ id: "#{item.model_name.singular.dasherize}-#{item.id}",
163
+ **row_attributes,
164
+ **YiffSpace.config.application_controller_class.helpers.data_attributes_for(item),
165
+ }
166
+ end
167
+ end
168
+ end
169
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module YiffSpace
4
+ module Tables
5
+ VERSION = "0.0.1"
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require("yiffspace/core")
4
+ require("zeitwerk")
5
+
6
+ loader = Zeitwerk::Loader.for_gem_extension(YiffSpace)
7
+ loader.setup
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yiffspace-tables
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Donovan_DMC
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2026-09-04 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: yiffspace-core
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: 0.2.0
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: 0.2.0
26
+ - !ruby/object:Gem::Dependency
27
+ name: zeitwerk
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '2.6'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '2.6'
40
+ description: HTML table-building DSL (YiffSpace::Tables::Builder), for https://yiff.space
41
+ and related projects
42
+ email:
43
+ - hewwo@yiff.rocks
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - CHANGELOG.md
49
+ - LICENSE
50
+ - README.md
51
+ - Rakefile
52
+ - lib/yiffspace/tables.rb
53
+ - lib/yiffspace/tables/builder.rb
54
+ - lib/yiffspace/tables/version.rb
55
+ homepage: https://yiff.space
56
+ licenses:
57
+ - MIT
58
+ metadata:
59
+ allowed_push_host: https://rubygems.org
60
+ homepage_uri: https://yiff.space
61
+ source_code_uri: https://github.com/YiffSpace/Gem/tree/master/yiffspace-tables
62
+ changelog_uri: https://github.com/YiffSpace/Gem/blob/master/yiffspace-tables/CHANGELOG.md
63
+ rubygems_mfa_required: 'true'
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 3.4.1
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubygems_version: 3.6.2
79
+ specification_version: 4
80
+ summary: HTML table-building DSL (YiffSpace::Tables::Builder), for https://yiff.space
81
+ and related projects
82
+ test_files: []