chardown_field 0.0.2
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/MIT-LICENSE +20 -0
- data/README.rdoc +45 -0
- data/Rakefile +37 -0
- data/app/assets/javascripts/chardown_field.js.coffee +14 -0
- data/app/helpers/chardown_form_extension.rb +36 -0
- data/config/routes.rb +2 -0
- data/lib/chardown_field.rb +28 -0
- data/lib/chardown_field/engine.rb +5 -0
- data/lib/chardown_field/version.rb +3 -0
- data/lib/tasks/chardown_field_tasks.rake +4 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a0f9a9b4af88041066e39a4ce9f6ba1780634355
|
4
|
+
data.tar.gz: 00080e60b6f80eda7bbb62de51e785bd056696e3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5d2cf59abab37045a2e5f85bfdc819527cd052e92f2812adb5a52eb9639e14b581285bb74f357f8651be55a747ca304046392be2cf0f6f545eea90ba0169dd99
|
7
|
+
data.tar.gz: 78a83cef58ad940feaf1221b7f03f0ab48976e06434eeeb48e0a20a808d6475d7da47ac1e1cb2718b397acfe21ce1b49c7b107f703c72494be26f593fe26f173
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2015 Damian Borowski
|
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.rdoc
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
= ChardownField
|
2
|
+
|
3
|
+
Extend your Rails forms with character countdown fields easy with just setting validations to your models.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
Installing this form extension is easy!
|
8
|
+
|
9
|
+
Add the following to your Gemfile:
|
10
|
+
|
11
|
+
gem 'chardown_field'
|
12
|
+
|
13
|
+
|
14
|
+
To your application.js:
|
15
|
+
|
16
|
+
//= require chardown_field
|
17
|
+
|
18
|
+
|
19
|
+
To any of your models with maximum characters validation setup:
|
20
|
+
|
21
|
+
Include ChardownField
|
22
|
+
|
23
|
+
|
24
|
+
And extend your forms by adding following attribute to your form_for/form_tag:
|
25
|
+
|
26
|
+
builder: ChardownFormExtension
|
27
|
+
|
28
|
+
Or set it up as default for all the form by adding following line to application_helper.rb:
|
29
|
+
|
30
|
+
ActionView::Base.default_form_builder = ChardownFormExtension
|
31
|
+
|
32
|
+
---
|
33
|
+
|
34
|
+
After above simple setup your form fields will show 'Characters left:' countdown right under them. Remember that it automatically detects your validations in your model on the fields you chose to validate.
|
35
|
+
|
36
|
+
You can style it to your needs by applying css to following classes:
|
37
|
+
|
38
|
+
.chardown-container # is a countdown wrapper and contains two elements:
|
39
|
+
|
40
|
+
|
41
|
+
span # where 'Characters left:' text is displayed
|
42
|
+
|
43
|
+
|
44
|
+
span.chardown # where you find number of left characters
|
45
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'ChardownField'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
|
20
|
+
|
21
|
+
load 'rails/tasks/statistics.rake'
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
Bundler::GemHelper.install_tasks
|
26
|
+
|
27
|
+
require 'rake/testtask'
|
28
|
+
|
29
|
+
Rake::TestTask.new(:test) do |t|
|
30
|
+
t.libs << 'lib'
|
31
|
+
t.libs << 'test'
|
32
|
+
t.pattern = 'test/**/*_test.rb'
|
33
|
+
t.verbose = false
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
task default: :test
|
@@ -0,0 +1,14 @@
|
|
1
|
+
$(document).ready ->
|
2
|
+
|
3
|
+
$('.chardown').each ->
|
4
|
+
chardownField = $(this)
|
5
|
+
closestTextInputField = $(this).parents('.chardown-container').siblings('textarea, input')
|
6
|
+
textInputFieldMaxLength = closestTextInputField.prop('maxlength')
|
7
|
+
textInputFieldCurrentLength = closestTextInputField.text().length
|
8
|
+
chardownField.text(textInputFieldMaxLength-textInputFieldCurrentLength)
|
9
|
+
|
10
|
+
updateTextInputField = ->
|
11
|
+
chardownField.text(textInputFieldMaxLength - closestTextInputField.val().length)
|
12
|
+
|
13
|
+
closestTextInputField.on 'keydown', updateTextInputField
|
14
|
+
closestTextInputField.on 'keyup', updateTextInputField
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# This is my custom_form_builder to extend text_field and text_area with characters countdown.
|
2
|
+
class ChardownFormExtension < ActionView::Helpers::FormBuilder
|
3
|
+
def text_area(label, *args)
|
4
|
+
if @object.respond_to? :maxlength_validations
|
5
|
+
if @object.maxlength_validations.keys.include?(label)
|
6
|
+
options = args.extract_options!
|
7
|
+
|
8
|
+
super(label, *(args << options.merge(maxlength: @object.maxlength_validations[label]))) + @template.content_tag('div',
|
9
|
+
@template.content_tag('span', 'Characters left: ') +
|
10
|
+
@template.content_tag('span', @object.maxlength_validations[label], class: "chardown", id:"#{@object_name.to_s+'_'+label.to_s}", style: 'text-align: right;'),
|
11
|
+
class: 'chardown-container', style: 'text-align: right;')
|
12
|
+
else
|
13
|
+
super(label, *args)
|
14
|
+
end
|
15
|
+
else
|
16
|
+
super(label, *args)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def text_field(label, *args)
|
21
|
+
if @object.respond_to? :maxlength_validations
|
22
|
+
if @object.maxlength_validations.keys.include?(label)
|
23
|
+
options = args.extract_options!
|
24
|
+
|
25
|
+
super(label, *(args << options.merge(maxlength: @object.maxlength_validations[label]))) + @template.content_tag('div',
|
26
|
+
@template.content_tag('span', 'Characters left: ') +
|
27
|
+
@template.content_tag('span', @object.maxlength_validations[label], class: "chardown", id:"#{@object_name.to_s+'_'+label.to_s}", style: 'text-align: right;'),
|
28
|
+
class: 'chardown-container', style: 'text-align: right;')
|
29
|
+
else
|
30
|
+
super(label, *args)
|
31
|
+
end
|
32
|
+
else
|
33
|
+
super(label, *args)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/config/routes.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require "chardown_field/engine"
|
2
|
+
|
3
|
+
module ChardownField
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
# I am going to set class and instance methods for now. Don't know which will
|
6
|
+
# be more helpful at the moment.
|
7
|
+
module ClassMethods
|
8
|
+
def maxlength_validations
|
9
|
+
|
10
|
+
validators.select{ |v|
|
11
|
+
v.class.to_s.include?('Length')
|
12
|
+
}.map{ |limit|
|
13
|
+
[ limit.attributes.first, limit.options[:maximum] ]
|
14
|
+
}.to_h
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def maxlength_validations
|
20
|
+
|
21
|
+
self.class.validators.select{ |v|
|
22
|
+
v.class.to_s.include?('Length')
|
23
|
+
}.map{ |limit|
|
24
|
+
[ limit.attributes.first, limit.options[:maximum] ]
|
25
|
+
}.to_h
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: chardown_field
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Damian Borowski
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-01 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: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: jquery
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
description: Extend your Rails forms with character countdown fields easy with just
|
42
|
+
setting validations to your models.
|
43
|
+
email:
|
44
|
+
- dbjrsy@gmail.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- MIT-LICENSE
|
50
|
+
- README.rdoc
|
51
|
+
- Rakefile
|
52
|
+
- app/assets/javascripts/chardown_field.js.coffee
|
53
|
+
- app/helpers/chardown_form_extension.rb
|
54
|
+
- config/routes.rb
|
55
|
+
- lib/chardown_field.rb
|
56
|
+
- lib/chardown_field/engine.rb
|
57
|
+
- lib/chardown_field/version.rb
|
58
|
+
- lib/tasks/chardown_field_tasks.rake
|
59
|
+
homepage: https://github.com/dbjrsy/ChardownField
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
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: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.2.2
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Extend your Rails forms with character countdown fields easy with just setting
|
83
|
+
validations to your models.
|
84
|
+
test_files: []
|