bard-tag_field 0.1.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.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/Appraisals +13 -0
- data/Gemfile +2 -0
- data/LICENSE +21 -0
- data/README.md +230 -0
- data/Rakefile +18 -0
- data/app/assets/javascripts/input-tag.js +1827 -0
- data/bard-tag/.gitignore +2 -0
- data/bard-tag/bun.lockb +0 -0
- data/bard-tag/index.js +1 -0
- data/bard-tag/package.json +15 -0
- data/bard-tag/rollup.config.js +19 -0
- data/bard-tag_field.gemspec +40 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/gemfiles/rails_7.1.gemfile +7 -0
- data/gemfiles/rails_7.2.gemfile +7 -0
- data/gemfiles/rails_8.0.gemfile +7 -0
- data/lib/bard/tag_field/field.rb +80 -0
- data/lib/bard/tag_field/form_builder.rb +33 -0
- data/lib/bard/tag_field/version.rb +7 -0
- data/lib/bard/tag_field.rb +20 -0
- metadata +124 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 99c36b23ef302095ac7b0c3994ac82027cc28df8efb1426097d67a639f735003
|
4
|
+
data.tar.gz: badb540eb22db036f7ee1d6930e440cffec862ca008c06e8b57aebb01b86cf50
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a0bdc6d674b71f7097dd8c6fa6f556f8a95dacd31560bcba2aed2dbb64a7cd94a22c71063e432b4d9f2e2e07b1b38fe2760fcced79a90cd7d9fb1c2935a9d123
|
7
|
+
data.tar.gz: 85891e20dbc60abf1292f2d091e93ef28cdc824d1110d18d54cec747ad7e54747a8ef52a9991f31c84d5f09b5ce3c42261edf42915d12a50905028d6b0e6f928
|
data/.rspec
ADDED
data/Appraisals
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Micah Geisel
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,230 @@
|
|
1
|
+
# Bard::TagField
|
2
|
+
|
3
|
+
[](https://github.com/botandrose/bard-tag_field/actions)
|
4
|
+
[](https://www.ruby-lang.org)
|
5
|
+
[](https://rubyonrails.org)
|
6
|
+
|
7
|
+
A Rails form helper gem that adds `bard_tag_field` to your forms, creating interactive tag input fields using the [@botandrose/input-tag](https://github.com/botandrose/input-tag) custom element.
|
8
|
+
|
9
|
+
Perfect for adding tag functionality to your Rails forms with a clean, modern interface that works seamlessly with your existing Rails form helpers.
|
10
|
+
|
11
|
+
## Features
|
12
|
+
|
13
|
+
- ๐ท๏ธ **Interactive tag input** - Users can add/remove tags dynamically
|
14
|
+
- ๐ง **Rails integration** - Works like any other Rails form helper (`form.select`, `form.text_field`, etc.)
|
15
|
+
- ๐จ **Customizable** - Supports all standard HTML options (class, id, data attributes)
|
16
|
+
- ๐ก๏ธ **Secure** - Automatic HTML escaping prevents XSS attacks
|
17
|
+
- ๐งช **Well-tested** - Comprehensive test suite
|
18
|
+
- โก **Modern** - Built with custom web elements
|
19
|
+
- ๐ **Compatible** - Supports Ruby 3.2+ and Rails 7.1+
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Basic Usage
|
24
|
+
|
25
|
+
After installing and requiring the gem, Use `bard_tag_field` in your Rails forms just like any other form helper:
|
26
|
+
|
27
|
+
```erb
|
28
|
+
<%= form_with model: @post do |form| %>
|
29
|
+
<%= form.bard_tag_field :tags %>
|
30
|
+
<% end %>
|
31
|
+
```
|
32
|
+
|
33
|
+
This generates an interactive tag field that binds to your model's `tags` attribute.
|
34
|
+
|
35
|
+
### With HTML Options
|
36
|
+
|
37
|
+
Add CSS classes, data attributes, and other HTML options:
|
38
|
+
|
39
|
+
```erb
|
40
|
+
<%= form.bard_tag_field :tags,
|
41
|
+
class: "form-control",
|
42
|
+
id: "post-tags",
|
43
|
+
data: { placeholder: "Add tags..." } %>
|
44
|
+
```
|
45
|
+
|
46
|
+
### With Existing Tags
|
47
|
+
|
48
|
+
The field automatically displays existing tags from your model:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
# In your controller
|
52
|
+
@post.tags = ["rails", "ruby", "web-development"]
|
53
|
+
```
|
54
|
+
|
55
|
+
```erb
|
56
|
+
<!-- Tags will be pre-populated in the form -->
|
57
|
+
<%= form.bard_tag_field :tags %>
|
58
|
+
```
|
59
|
+
|
60
|
+
### With Predefined Choices (Rails select-style)
|
61
|
+
|
62
|
+
Like `form.select`, you can provide predefined choices for users to select from:
|
63
|
+
|
64
|
+
```erb
|
65
|
+
<%= form.bard_tag_field :tags, ["ruby", "rails", "javascript", "css"] %>
|
66
|
+
```
|
67
|
+
|
68
|
+
Or use nested arrays for display vs submit values:
|
69
|
+
|
70
|
+
```erb
|
71
|
+
<%= form.bard_tag_field :categories, [
|
72
|
+
["Web Development", "web-dev"],
|
73
|
+
["Machine Learning", "ml"],
|
74
|
+
["Database Design", "db"]
|
75
|
+
] %>
|
76
|
+
```
|
77
|
+
|
78
|
+
This creates a datalist with available options while still showing current object values as selected tags.
|
79
|
+
|
80
|
+
### Custom Content with Blocks
|
81
|
+
|
82
|
+
Use blocks for custom tag rendering:
|
83
|
+
|
84
|
+
```erb
|
85
|
+
<%= form.bard_tag_field :tags do |options| %>
|
86
|
+
<% @post.tags.each do |tag| %>
|
87
|
+
<tag-option value="<%= tag %>" class="custom-tag"><%= tag %></tag-option>
|
88
|
+
<% end %>
|
89
|
+
<% end %>
|
90
|
+
```
|
91
|
+
|
92
|
+
## Model Setup
|
93
|
+
|
94
|
+
Your model should handle tags as an array. Here are common approaches:
|
95
|
+
|
96
|
+
### With Array Attribute (Rails 5+)
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
class Post < ApplicationRecord
|
100
|
+
# In migration: add_column :posts, :tags, :text, array: true, default: []
|
101
|
+
# Or use JSON column: add_column :posts, :tags, :json, default: []
|
102
|
+
end
|
103
|
+
```
|
104
|
+
|
105
|
+
### With Serialization
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
class Post < ApplicationRecord
|
109
|
+
serialize :tags, Array
|
110
|
+
end
|
111
|
+
```
|
112
|
+
|
113
|
+
### With ActsAsTaggable
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
class Post < ApplicationRecord
|
117
|
+
acts_as_taggable_on :tags
|
118
|
+
|
119
|
+
# Helper method for form binding
|
120
|
+
def tags_array
|
121
|
+
tag_list.to_a
|
122
|
+
end
|
123
|
+
|
124
|
+
def tags_array=(values)
|
125
|
+
self.tag_list = values
|
126
|
+
end
|
127
|
+
end
|
128
|
+
```
|
129
|
+
|
130
|
+
```erb
|
131
|
+
<%= form.bard_tag_field :tags_array %>
|
132
|
+
```
|
133
|
+
|
134
|
+
## Generated HTML
|
135
|
+
|
136
|
+
The gem generates semantic HTML using custom elements:
|
137
|
+
|
138
|
+
```html
|
139
|
+
<!-- With current object values -->
|
140
|
+
<input-tag name="post[tags]" id="post_tags">
|
141
|
+
<tag-option value="rails">rails</tag-option>
|
142
|
+
<tag-option value="ruby">ruby</tag-option>
|
143
|
+
</input-tag>
|
144
|
+
|
145
|
+
<!-- With choices parameter -->
|
146
|
+
<input-tag name="post[tags]" id="post_tags" list="post_tags_datalist">
|
147
|
+
<tag-option value="rails">rails</tag-option>
|
148
|
+
</input-tag>
|
149
|
+
<datalist id="post_tags_datalist">
|
150
|
+
<option value="ruby">ruby</option>
|
151
|
+
<option value="javascript">javascript</option>
|
152
|
+
<option value="css">css</option>
|
153
|
+
</datalist>
|
154
|
+
```
|
155
|
+
|
156
|
+
## JavaScript Integration
|
157
|
+
|
158
|
+
This gem works with the [@botandrose/input-tag](https://github.com/botandrose/input-tag) custom element. Make sure to include it in your asset pipeline:
|
159
|
+
|
160
|
+
```javascript
|
161
|
+
// In your application.js or wherever you manage JS
|
162
|
+
import '@botandrose/input-tag'
|
163
|
+
```
|
164
|
+
|
165
|
+
Or include the precompiled asset (automatically added by this gem):
|
166
|
+
|
167
|
+
```javascript
|
168
|
+
//= require input-tag
|
169
|
+
```
|
170
|
+
|
171
|
+
## Browser Support
|
172
|
+
|
173
|
+
- Modern browsers that support custom elements
|
174
|
+
- Graceful degradation for older browsers
|
175
|
+
- Supports Ruby 3.2+ and Rails 7.1+ (including Rails 8.0)
|
176
|
+
|
177
|
+
## API Reference
|
178
|
+
|
179
|
+
### `bard_tag_field(method, choices = nil, options = {}, html_options = {}, &block)`
|
180
|
+
|
181
|
+
**Parameters:**
|
182
|
+
- `method` - The attribute name (symbol)
|
183
|
+
- `choices` - Optional array of predefined choices (like `form.select`)
|
184
|
+
- `options` - Hash of Rails form options
|
185
|
+
- `html_options` - Hash of HTML attributes (class, id, data, etc.)
|
186
|
+
- `&block` - Optional block for custom content rendering
|
187
|
+
|
188
|
+
**Returns:** HTML-safe string containing the tag input element
|
189
|
+
|
190
|
+
**Examples:**
|
191
|
+
```ruby
|
192
|
+
# Basic usage
|
193
|
+
form.bard_tag_field :tags
|
194
|
+
|
195
|
+
# With choices
|
196
|
+
form.bard_tag_field :tags, ["ruby", "rails", "javascript"]
|
197
|
+
|
198
|
+
# With nested choices (display vs value)
|
199
|
+
form.bard_tag_field :categories, [["Web Dev", "web"], ["ML", "ml"]]
|
200
|
+
|
201
|
+
# With HTML options
|
202
|
+
form.bard_tag_field :tags, class: "form-control", data: { max_tags: 5 }
|
203
|
+
|
204
|
+
# With choices and HTML options
|
205
|
+
form.bard_tag_field :tags, ["ruby", "rails"], {}, { class: "form-control" }
|
206
|
+
```
|
207
|
+
|
208
|
+
## Development
|
209
|
+
|
210
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rspec` to run the tests.
|
211
|
+
|
212
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
213
|
+
|
214
|
+
## Contributing
|
215
|
+
|
216
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/botandrose/bard-tag_field.
|
217
|
+
|
218
|
+
1. Fork it
|
219
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
220
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
221
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
222
|
+
5. Create new Pull Request
|
223
|
+
|
224
|
+
## License
|
225
|
+
|
226
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
227
|
+
|
228
|
+
## Credits
|
229
|
+
|
230
|
+
Created by [Micah Geisel](https://github.com/micahgeisel) at [Bot & Rose](https://github.com/botandrose).
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rspec/core/rake_task"
|
5
|
+
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
7
|
+
|
8
|
+
task default: :spec
|
9
|
+
|
10
|
+
desc "Build JavaScript assets"
|
11
|
+
task :build_js do
|
12
|
+
sh "cd bard-tag && npm run build"
|
13
|
+
end
|
14
|
+
|
15
|
+
desc "Install npm dependencies"
|
16
|
+
task :install_deps do
|
17
|
+
sh "cd bard-tag && npm install"
|
18
|
+
end
|