signature_pad_rails 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/README.md +281 -0
- data/Rakefile +8 -0
- data/app/models/signature_pad_rails/signature.rb +9 -0
- data/app/views/signature_pad_rails/_signature_pad.html.erb +15 -0
- data/lib/generators/signature_pad_rails/install/install_generator.rb +52 -0
- data/lib/generators/signature_pad_rails/templates/migration.rb +11 -0
- data/lib/signature_pad_rails/signable.rb +15 -0
- data/lib/signature_pad_rails/version.rb +5 -0
- data/lib/signature_pad_rails.rb +17 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 392312070b7cae8b1cfdb32d39adcc84025295c002bcec9f3adc83343dd5f151
|
4
|
+
data.tar.gz: 581ab90b67234b0116a1f8086bde505467ffdff5ce2e09817ff654253db9be6a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e78879a866ccba70f05a03cf708130c77dd459a8d6c0dab13b70cd1867a723799d94abbee4764084602bbac193c1ddad2dbf60a476e8fbb14e237fe0c7c2fa82
|
7
|
+
data.tar.gz: 5488b5d21a21c3f5b16ea3f97820b8e86bf5b493fd30d72066526cbfe64f1ae2d4189e9610ece7210c0bc389346289db0df401d88282b42a6df79654819030e3
|
data/README.md
ADDED
@@ -0,0 +1,281 @@
|
|
1
|
+
# SignaturePadRails
|
2
|
+
|
3
|
+
Easy integration of signature_pad.js with Rails applications.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'signature_pad_rails'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
```bash
|
15
|
+
$ bundle install
|
16
|
+
$ rails generate signature_pad_rails:install
|
17
|
+
$ rails db:migrate
|
18
|
+
```
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
### 1. Include the concern in your model:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
class Swmm < ApplicationRecord
|
26
|
+
include SignaturePadRails::Signable
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
### 2. Add the signature pad to your form:
|
31
|
+
|
32
|
+
```erb
|
33
|
+
<%= render 'signature_pad_rails/signature_pad', signable: @swmm %>
|
34
|
+
```
|
35
|
+
|
36
|
+
### 3. Import the JavaScript in your application.js:
|
37
|
+
|
38
|
+
```javascript
|
39
|
+
import "signature_pad_rails"
|
40
|
+
```
|
41
|
+
|
42
|
+
### 4. Permit signature attributes in your controller:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
def swmm_params
|
46
|
+
params.require(:swmm).permit(
|
47
|
+
# other params...
|
48
|
+
signature_attributes: [:data]
|
49
|
+
)
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
### 5. Display signatures:
|
54
|
+
|
55
|
+
```erb
|
56
|
+
<% if @swmm.signature.present? %>
|
57
|
+
<div class="signature-display">
|
58
|
+
<%= image_tag @swmm.signature.data, alt: "Signature" %>
|
59
|
+
</div>
|
60
|
+
<% end %>
|
61
|
+
```
|
62
|
+
|
63
|
+
## Configuration
|
64
|
+
|
65
|
+
You can configure SignaturePadRails in an initializer:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
# config/initializers/signature_pad_rails.rb
|
69
|
+
SignaturePadRails.configure do |config|
|
70
|
+
config.pad_width = 400
|
71
|
+
config.pad_height = 200
|
72
|
+
config.background_color = 'rgb(255, 255, 255)'
|
73
|
+
config.pen_color = 'rgb(0, 0, 0)'
|
74
|
+
config.min_width = 0.5
|
75
|
+
config.max_width = 2.5
|
76
|
+
config.throttle = 16
|
77
|
+
config.min_distance = 5
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
## Advanced Usage
|
82
|
+
|
83
|
+
### Custom Styling
|
84
|
+
|
85
|
+
Add custom CSS to style the signature pad:
|
86
|
+
|
87
|
+
```css
|
88
|
+
.signature-pad-container {
|
89
|
+
width: 100%;
|
90
|
+
max-width: 600px;
|
91
|
+
margin: 0 auto;
|
92
|
+
}
|
93
|
+
|
94
|
+
.signature-pad {
|
95
|
+
border: 2px solid #ddd;
|
96
|
+
border-radius: 8px;
|
97
|
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
98
|
+
}
|
99
|
+
|
100
|
+
.signature-pad-actions {
|
101
|
+
margin-top: 1rem;
|
102
|
+
display: flex;
|
103
|
+
justify-content: space-between;
|
104
|
+
}
|
105
|
+
```
|
106
|
+
|
107
|
+
### Multiple Signatures
|
108
|
+
|
109
|
+
For models with multiple signatures:
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
class Document < ApplicationRecord
|
113
|
+
has_many :signatures,
|
114
|
+
class_name: 'SignaturePadRails::Signature',
|
115
|
+
as: :signable,
|
116
|
+
dependent: :destroy
|
117
|
+
end
|
118
|
+
```
|
119
|
+
|
120
|
+
### Validation
|
121
|
+
|
122
|
+
Add custom validation to ensure signatures are provided:
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
class Swmm < ApplicationRecord
|
126
|
+
include SignaturePadRails::Signable
|
127
|
+
|
128
|
+
validates :signature, presence: true, on: :update
|
129
|
+
end
|
130
|
+
```
|
131
|
+
|
132
|
+
## API Reference
|
133
|
+
|
134
|
+
### SignaturePadRails::Signable
|
135
|
+
|
136
|
+
The main concern that adds signature functionality to your models.
|
137
|
+
|
138
|
+
#### Methods
|
139
|
+
|
140
|
+
- `signature` - Returns the associated signature
|
141
|
+
- `signature_present?` - Returns true if a signature exists
|
142
|
+
- `signature_data` - Returns the raw signature data
|
143
|
+
|
144
|
+
### SignaturePadRails::Signature
|
145
|
+
|
146
|
+
The signature model that stores signature data.
|
147
|
+
|
148
|
+
#### Attributes
|
149
|
+
|
150
|
+
- `data` - Base64 encoded signature image
|
151
|
+
- `signable` - Polymorphic association to the parent model
|
152
|
+
- `created_at` - Timestamp when signature was created
|
153
|
+
- `updated_at` - Timestamp when signature was last updated
|
154
|
+
|
155
|
+
## JavaScript API
|
156
|
+
|
157
|
+
The gem provides JavaScript functionality that can be customized:
|
158
|
+
|
159
|
+
```javascript
|
160
|
+
// Get the signature pad instance
|
161
|
+
const signaturePad = document.querySelector('#signature-pad').signaturePad;
|
162
|
+
|
163
|
+
// Clear the pad programmatically
|
164
|
+
signaturePad.clear();
|
165
|
+
|
166
|
+
// Check if empty
|
167
|
+
if (signaturePad.isEmpty()) {
|
168
|
+
console.log('No signature');
|
169
|
+
}
|
170
|
+
|
171
|
+
// Get signature data
|
172
|
+
const data = signaturePad.toDataURL();
|
173
|
+
```
|
174
|
+
|
175
|
+
## Turbo Support
|
176
|
+
|
177
|
+
The gem is fully compatible with Turbo. For forms submitted via Turbo:
|
178
|
+
|
179
|
+
```erb
|
180
|
+
<%= form_with model: @swmm, data: { turbo: true } do |form| %>
|
181
|
+
<%= render 'signature_pad_rails/signature_pad', signable: @swmm, form: form %>
|
182
|
+
<% end %>
|
183
|
+
```
|
184
|
+
|
185
|
+
## Stimulus Integration
|
186
|
+
|
187
|
+
For custom Stimulus controllers:
|
188
|
+
|
189
|
+
```javascript
|
190
|
+
import { Controller } from "@hotwired/stimulus"
|
191
|
+
import SignaturePad from "signature_pad"
|
192
|
+
|
193
|
+
export default class extends Controller {
|
194
|
+
static targets = [ "canvas", "input" ]
|
195
|
+
|
196
|
+
connect() {
|
197
|
+
this.signaturePad = new SignaturePad(this.canvasTarget)
|
198
|
+
}
|
199
|
+
|
200
|
+
clear() {
|
201
|
+
this.signaturePad.clear()
|
202
|
+
}
|
203
|
+
|
204
|
+
save() {
|
205
|
+
if (!this.signaturePad.isEmpty()) {
|
206
|
+
this.inputTarget.value = this.signaturePad.toDataURL()
|
207
|
+
}
|
208
|
+
}
|
209
|
+
}
|
210
|
+
```
|
211
|
+
|
212
|
+
## Troubleshooting
|
213
|
+
|
214
|
+
### Common Issues
|
215
|
+
|
216
|
+
1. **Signature not saving**: Ensure you've added `signature_attributes: [:data]` to your strong parameters.
|
217
|
+
|
218
|
+
2. **JavaScript not loading**: Make sure you've run `yarn add signature_pad` and imported the JavaScript.
|
219
|
+
|
220
|
+
3. **Canvas sizing issues**: The canvas may need to be resized on window resize:
|
221
|
+
|
222
|
+
```javascript
|
223
|
+
window.addEventListener('resize', () => {
|
224
|
+
// Resize canvas logic
|
225
|
+
});
|
226
|
+
```
|
227
|
+
|
228
|
+
## Contributing
|
229
|
+
|
230
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/yourusername/signature_pad_rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/yourusername/signature_pad_rails/blob/main/CODE_OF_CONDUCT.md).
|
231
|
+
|
232
|
+
1. Fork it
|
233
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
234
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
235
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
236
|
+
5. Create new Pull Request
|
237
|
+
|
238
|
+
## Development
|
239
|
+
|
240
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
241
|
+
|
242
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
243
|
+
|
244
|
+
## Testing
|
245
|
+
|
246
|
+
```bash
|
247
|
+
bundle exec rspec
|
248
|
+
```
|
249
|
+
|
250
|
+
To run tests with coverage:
|
251
|
+
|
252
|
+
```bash
|
253
|
+
COVERAGE=true bundle exec rspec
|
254
|
+
```
|
255
|
+
|
256
|
+
## License
|
257
|
+
|
258
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
259
|
+
|
260
|
+
## Code of Conduct
|
261
|
+
|
262
|
+
Everyone interacting in the SignaturePadRails project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/yourusername/signature_pad_rails/blob/main/CODE_OF_CONDUCT.md).
|
263
|
+
|
264
|
+
## Credits
|
265
|
+
|
266
|
+
This gem is built on top of the excellent [signature_pad](https://github.com/szimek/signature_pad) library by Szymon Nowak.
|
267
|
+
|
268
|
+
## Support
|
269
|
+
|
270
|
+
For bug reports and feature requests, please use the [GitHub issue tracker](https://github.com/yourusername/signature_pad_rails/issues).
|
271
|
+
|
272
|
+
## Changelog
|
273
|
+
|
274
|
+
See [CHANGELOG.md](CHANGELOG.md) for a list of changes.
|
275
|
+
|
276
|
+
## Requirements
|
277
|
+
|
278
|
+
- Ruby 2.7.0 or higher
|
279
|
+
- Rails 7.0.0 or higher
|
280
|
+
- Modern browser with canvas support
|
281
|
+
- Node.js and Yarn (for JavaScript dependencies)
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# app/views/signature_pad_rails/_signature_pad.html.erb
|
2
|
+
<div class="signature-pad-container">
|
3
|
+
<canvas id="signature-pad" class="signature-pad" width="400" height="200"></canvas>
|
4
|
+
|
5
|
+
<%= form_with(model: signable, local: true, id: 'signature-form') do |form| %>
|
6
|
+
<%= form.fields_for :signature do |signature_form| %>
|
7
|
+
<%= signature_form.hidden_field :data, id: 'signature_data' %>
|
8
|
+
<% end %>
|
9
|
+
|
10
|
+
<div class="signature-pad-actions">
|
11
|
+
<button id="clear-signature" class="btn btn-secondary">Clear</button>
|
12
|
+
<button id="save-signature" class="btn btn-primary">Save</button>
|
13
|
+
</div>
|
14
|
+
<% end %>
|
15
|
+
</div>
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# lib/generators/signature_pad_rails/install/install_generator.rb
|
2
|
+
module SignaturePadRails
|
3
|
+
module Generators
|
4
|
+
class InstallGenerator < Rails::Generators::Base
|
5
|
+
source_root File.expand_path("templates", __dir__)
|
6
|
+
|
7
|
+
def create_migrations
|
8
|
+
rake "signature_pad_rails:install:migrations"
|
9
|
+
end
|
10
|
+
|
11
|
+
def create_initializer
|
12
|
+
template "initializer.rb", "config/initializers/signature_pad_rails.rb"
|
13
|
+
end
|
14
|
+
|
15
|
+
def install_javascript
|
16
|
+
create_file "app/javascript/signature_pad_rails.js", <<-JS
|
17
|
+
import SignaturePad from 'signature_pad'
|
18
|
+
|
19
|
+
document.addEventListener('turbo:load', () => {
|
20
|
+
const canvas = document.querySelector('#signature-pad')
|
21
|
+
|
22
|
+
if (canvas) {
|
23
|
+
const signaturePad = new SignaturePad(canvas, {
|
24
|
+
backgroundColor: 'rgb(255, 255, 255)'
|
25
|
+
})
|
26
|
+
|
27
|
+
document.querySelector('#clear-signature').addEventListener('click', (e) => {
|
28
|
+
e.preventDefault()
|
29
|
+
signaturePad.clear()
|
30
|
+
})
|
31
|
+
|
32
|
+
document.querySelector('#save-signature').addEventListener('click', (e) => {
|
33
|
+
e.preventDefault()
|
34
|
+
if (signaturePad.isEmpty()) {
|
35
|
+
alert('Please provide a signature first.')
|
36
|
+
} else {
|
37
|
+
const signatureData = signaturePad.toDataURL()
|
38
|
+
document.querySelector('#signature_data').value = signatureData
|
39
|
+
document.querySelector('#signature-form').submit()
|
40
|
+
}
|
41
|
+
})
|
42
|
+
}
|
43
|
+
})
|
44
|
+
JS
|
45
|
+
end
|
46
|
+
|
47
|
+
def add_yarn_package
|
48
|
+
run "yarn add signature_pad"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# lib/generators/signature_pad_rails/templates/migration.rb
|
2
|
+
class CreateSignaturePadRailsSignatures < ActiveRecord::Migration[7.0]
|
3
|
+
def change
|
4
|
+
create_table :signature_pad_rails_signatures do |t|
|
5
|
+
t.text :data
|
6
|
+
t.references :signable, polymorphic: true, null: false
|
7
|
+
|
8
|
+
t.timestamps
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# lib/signature_pad_rails/signable.rb
|
2
|
+
module SignaturePadRails
|
3
|
+
module Signable
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
has_one :signature,
|
8
|
+
class_name: 'SignaturePadRails::Signature',
|
9
|
+
as: :signable,
|
10
|
+
dependent: :destroy
|
11
|
+
|
12
|
+
accepts_nested_attributes_for :signature
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# lib/signature_pad_rails.rb
|
2
|
+
require "signature_pad_rails/version"
|
3
|
+
require "signature_pad_rails/engine"
|
4
|
+
|
5
|
+
module SignaturePadRails
|
6
|
+
end
|
7
|
+
|
8
|
+
# lib/signature_pad_rails/engine.rb
|
9
|
+
module SignaturePadRails
|
10
|
+
class Engine < ::Rails::Engine
|
11
|
+
isolate_namespace SignaturePadRails
|
12
|
+
|
13
|
+
initializer "signature_pad_rails.assets" do |app|
|
14
|
+
app.config.assets.precompile += %w( signature_pad_rails/signature_pad.js )
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: signature_pad_rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- heyjeremy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-06-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 7.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 7.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: jsbundling-rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Easy integration of signature_pad.js with Rails, including models and
|
42
|
+
views
|
43
|
+
email:
|
44
|
+
- hello@heyjeremy.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- app/models/signature_pad_rails/signature.rb
|
52
|
+
- app/views/signature_pad_rails/_signature_pad.html.erb
|
53
|
+
- lib/generators/signature_pad_rails/install/install_generator.rb
|
54
|
+
- lib/generators/signature_pad_rails/templates/migration.rb
|
55
|
+
- lib/signature_pad_rails.rb
|
56
|
+
- lib/signature_pad_rails/signable.rb
|
57
|
+
- lib/signature_pad_rails/version.rb
|
58
|
+
homepage: https://github.com/ohheyjeremy/SignaturePadRails
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata:
|
62
|
+
homepage_uri: https://github.com/ohheyjeremy/SignaturePadRails
|
63
|
+
source_code_uri: https://github.com/ohheyjeremy/SignaturePadRails
|
64
|
+
changelog_uri: https://github.com/ohheyjeremy/SignaturePadRails/blob/main/CHANGELOG.md
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubygems_version: 3.3.26
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Signature pad integration for Rails applications
|
84
|
+
test_files: []
|