Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/active_hashcash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,16 @@ def hashcash_after_success

# Call it inside the form that have to be protected and don't forget to initialize the JavaScript Hascash.setup().
# Unless you need something really special, you should not need to override this method.
def hashcash_hidden_field_tag(name = :hashcash)
options = {resource: hashcash_resource, bits: hashcash_bits, waiting_message: hashcash_waiting_message}
view_context.hidden_field_tag(name, "", "data-hashcash" => options.to_json)
def hashcash_hidden_field_tag(name = :hashcash, **html_attributes)
options = {
resource: hashcash_resource,
bits: hashcash_bits,
waiting_message: hashcash_waiting_message
}
html_attributes[:id] ||= "hashcash_#{SecureRandom.hex(4)}"
html_attributes["data-hashcash"] ||= options.to_json
html_attributes["data-controller"] ||= "hashcash"

view_context.hidden_field_tag(name, "", **html_attributes)
end
end
2 changes: 1 addition & 1 deletion lib/hashcash.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Hashcash = function(input) {

Hashcash.setup = function() {
if (document.readyState != "loading") {
var input = document.querySelector("input#hashcash")
var input = document.querySelector("input[id^=hashcash]")
input && new Hashcash(input)
} else
document.addEventListener("DOMContentLoaded", Hashcash.setup )
Expand Down