simphi 0.0.2 → 0.0.3
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 +4 -4
- data/Gemfile +5 -0
- data/README.md +58 -4
- data/Rakefile +13 -3
- data/lib/simphi.rb +4 -0
- data/lib/simphi/request.rb +4 -4
- data/lib/simphi/simphi_input.rb +167 -0
- data/simphi.gemspec +4 -4
- data/spec/request_spec.rb +4 -4
- data/spec/simphi_input_spec.rb +172 -0
- data/spec/spec_helper.rb +10 -3
- data/spec/support/input.rb +15 -0
- data/vendor/assets/javascripts/simphi.js.coffee +47 -0
- data/vendor/assets/stylesheets/simphi.css +3 -0
- metadata +35 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9a1799995a0418694980372ae82a18e3323d165a
|
4
|
+
data.tar.gz: b8ae84379e727ed40a3682c4e664a57353b22a92
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 58ffc898a6c3372b1383c9d1f65dc90e057aeb8704119a376cd7c6b961e516482748cbff570bf9a949cc7332378e34419a302a8a52888059953d927fdd89768d
|
7
|
+
data.tar.gz: 19283f96b2e418323c79c823d1b106cc339912721983bb304b935c5b33cd2af0cfbc21eed293612d9d0af2c4bf43d6c06255f6412a58cd76ad961b72a0eb7ebb
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,52 @@
|
|
1
1
|
# Simphi
|
2
2
|
|
3
|
-
|
3
|
+
## Description
|
4
|
+
|
5
|
+
This gem were implemented for natural using hashes in web development.
|
6
|
+
The gem divided in two parts: middleware and simple_form custom input.
|
7
|
+
With *Simphi::Middleware* you can send params to server and they will be rebuilt to normal hash and with simple_form input the hash can be sended in proper format to server.
|
8
|
+
|
9
|
+
### Middleware
|
10
|
+
The middleware relies to `-simphi` part of the hash key.
|
11
|
+
#### Example
|
12
|
+
```ruby
|
13
|
+
"contacts-simphi": {
|
14
|
+
"don't matter what here": {
|
15
|
+
"key": "email",
|
16
|
+
"value": "beznosa@yahoo.com"
|
17
|
+
},
|
18
|
+
"don't matter what here too": {
|
19
|
+
"key": "skype",
|
20
|
+
"value": "mix_beznos"
|
21
|
+
}
|
22
|
+
}
|
23
|
+
|
24
|
+
# will be replaced with:
|
25
|
+
|
26
|
+
"contacts": {
|
27
|
+
"email": "beznosa@yahoo.com"
|
28
|
+
},
|
29
|
+
"skype": "mix_beznos"
|
30
|
+
}
|
31
|
+
}
|
32
|
+
```
|
33
|
+
|
34
|
+
### Hash Input
|
35
|
+
SimphiInput is a SimpleForm custom input which create a typical hash structure for input with key, value.
|
36
|
+
|
37
|
+
[Simphi Hash Input](http://gdurl.com/wTNH)
|
38
|
+
|
39
|
+
It can be used as usual custom input:
|
40
|
+
```ruby
|
41
|
+
= simple_form_for do |f|
|
42
|
+
= f.input :contacts, as: :simphi
|
43
|
+
# Required fields can be passed as array of symbols or single symbol.
|
44
|
+
# It works as field which will be presetted even if 'contacts' will be empty.
|
45
|
+
= f.input :contacts, as: :simphi, required_fields: [:phone, :skype]
|
46
|
+
# Also it can be populated with options for key_input, value_input, remove_button, error, add_button
|
47
|
+
= f.input :contacts, as: :simphi, key_input: { class: 'shi_key' }, add_button: { id: 'custom_button_id' }
|
48
|
+
```
|
49
|
+
|
4
50
|
|
5
51
|
## Installation
|
6
52
|
|
@@ -18,13 +64,21 @@ Or install it yourself as:
|
|
18
64
|
|
19
65
|
$ gem install simphi
|
20
66
|
|
21
|
-
|
67
|
+
To use middleware you need to add such lines to your `config/application.rb` file(in case of using rails):
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
require "simphi/middleware"
|
71
|
+
|
72
|
+
class Application < Rails::Application
|
73
|
+
config.middleware.use Simphi::Middleware
|
74
|
+
end
|
75
|
+
```
|
22
76
|
|
23
|
-
|
77
|
+
To use hash input can be used `as: :simphi` option for simple form input.
|
24
78
|
|
25
79
|
## Contributing
|
26
80
|
|
27
|
-
1. Fork it ( https://github.com/
|
81
|
+
1. Fork it ( https://github.com/AlexBeznos/simphi/fork )
|
28
82
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
83
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
84
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/Rakefile
CHANGED
@@ -1,5 +1,15 @@
|
|
1
|
-
|
1
|
+
#!/usr/bin/env rake
|
2
2
|
|
3
|
-
|
3
|
+
begin
|
4
|
+
require 'bundler/setup'
|
5
|
+
rescue LoadError
|
6
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
7
|
+
end
|
4
8
|
|
5
|
-
|
9
|
+
require 'rspec/core'
|
10
|
+
require 'rspec/core/rake_task'
|
11
|
+
|
12
|
+
Bundler::GemHelper.install_tasks
|
13
|
+
RSpec::Core::RakeTask.new(:spec)
|
14
|
+
|
15
|
+
task :default => :spec
|
data/lib/simphi.rb
CHANGED
data/lib/simphi/request.rb
CHANGED
@@ -8,7 +8,7 @@ module Simphi
|
|
8
8
|
|
9
9
|
if with_hash? key
|
10
10
|
param = delete_param(key)
|
11
|
-
update_param(key.gsub(/-
|
11
|
+
update_param(key.gsub(/-simphi/, ''), normalized_hash(param))
|
12
12
|
end
|
13
13
|
|
14
14
|
|
@@ -18,8 +18,8 @@ module Simphi
|
|
18
18
|
private
|
19
19
|
|
20
20
|
def with_hash?(param)
|
21
|
-
return param =~ /-
|
22
|
-
return param.to_s =~ /-
|
21
|
+
return param =~ /-simphi/ if param.is_a? String
|
22
|
+
return param.to_s =~ /-simphi/ if param.is_a? Hash
|
23
23
|
end
|
24
24
|
|
25
25
|
def normalized_hash(params)
|
@@ -43,7 +43,7 @@ module Simphi
|
|
43
43
|
if with_hash? k
|
44
44
|
{
|
45
45
|
key: k,
|
46
|
-
obj: { k.gsub(/-
|
46
|
+
obj: { k.gsub(/-simphi/, '') => normalized_hash(v) }
|
47
47
|
}
|
48
48
|
end
|
49
49
|
|
@@ -0,0 +1,167 @@
|
|
1
|
+
require 'simple_form'
|
2
|
+
|
3
|
+
####
|
4
|
+
# Simple Hash extending for code minimization
|
5
|
+
class Hash
|
6
|
+
def exclude_keys(*keys)
|
7
|
+
self.reject { |e| keys.include? e }
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module Simphi
|
12
|
+
class SimphiInput < SimpleForm::Inputs::Base
|
13
|
+
def input(wrapper_options)
|
14
|
+
@io = ActiveSupport::SafeBuffer.new
|
15
|
+
|
16
|
+
@builder.fields_for "#{attribute_name}-shi_hash" do |b|
|
17
|
+
@hash_builder = b
|
18
|
+
|
19
|
+
@io.tap do |sb|
|
20
|
+
get_fields_for(required_attributes, 1, true)
|
21
|
+
get_fields_for(not_required_attributes, required_attributes.count + 1)
|
22
|
+
add_button
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
def attribute_value
|
30
|
+
object.send(attribute_name)
|
31
|
+
end
|
32
|
+
|
33
|
+
def required_attributes
|
34
|
+
Array.new([ input_options.try(:[], :required_fields) ])
|
35
|
+
.flatten
|
36
|
+
.compact
|
37
|
+
.map(&:to_s)
|
38
|
+
end
|
39
|
+
|
40
|
+
def not_required_attributes
|
41
|
+
attribute_value.try(:keys).to_a - required_attributes + [:sample_shi_hash]
|
42
|
+
end
|
43
|
+
|
44
|
+
def get_fields_for(array, start, required = false)
|
45
|
+
array.each_with_index.each do |key, index|
|
46
|
+
@io << hash_pair(key, start + index, @hash_builder)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
####
|
51
|
+
# Button for adding new hash pairs
|
52
|
+
def add_button
|
53
|
+
@io << template.content_tag(:button, *add_button_options)
|
54
|
+
end
|
55
|
+
|
56
|
+
def add_button_options
|
57
|
+
opts = input_options_for(:add_button)
|
58
|
+
[
|
59
|
+
opts.fetch(:label, I18n.t('simple_hash_input.add_button', default: 'Add more')),
|
60
|
+
{
|
61
|
+
class: "#{opts.fetch(:class, 'btn btn-block btn-primary')} add_hash_pair"
|
62
|
+
}.merge(opts.exclude_keys(:label, :class))
|
63
|
+
]
|
64
|
+
end
|
65
|
+
|
66
|
+
####
|
67
|
+
# Hash pair is inputs for key, value of hash, also it is added remove button
|
68
|
+
# and block for errors handling
|
69
|
+
def hash_pair(key, index, builder)
|
70
|
+
template.content_tag(:div, hash_pair_options(key, index)) do
|
71
|
+
builder.fields_for "hash-#{index}" do |f|
|
72
|
+
template.concat( wrapped_input(key, f, 'key') )
|
73
|
+
template.concat( wrapped_input(key, f, 'value') )
|
74
|
+
template.concat( remove_button() )
|
75
|
+
template.concat( error_help_block(key) )
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def hash_pair_options(key, index)
|
81
|
+
opts = input_options_for(:hash_pair)
|
82
|
+
klass = ["hash-#{index}"]
|
83
|
+
klass << opts.fetch(:class, 'row hash-pair')
|
84
|
+
klass << 'sample' if key == :sample_shi_hash
|
85
|
+
klass << 'has_error' if error(key)
|
86
|
+
klass * ' '
|
87
|
+
|
88
|
+
{
|
89
|
+
class: klass
|
90
|
+
}.merge(opts.exclude_keys(:class))
|
91
|
+
end
|
92
|
+
|
93
|
+
####
|
94
|
+
# Input field, wrapped into block for better styling ability
|
95
|
+
def wrapped_input(key, builder, name)
|
96
|
+
opts = input_options_for("#{name}_input".to_sym).fetch(:wrapper, {})
|
97
|
+
wrapper_opts = { class: 'col-xs-5' }.merge(opts)
|
98
|
+
|
99
|
+
template.content_tag(:div, wrapper_opts) do
|
100
|
+
builder.text_field(name, self.send("#{name}_options", key))
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def key_options(key)
|
105
|
+
extends = if key == :sample_shi_hash
|
106
|
+
{ disabled: 'disabled' }
|
107
|
+
else
|
108
|
+
{ value: key }
|
109
|
+
end
|
110
|
+
|
111
|
+
input_options_for(:key_input).merge(extends)
|
112
|
+
end
|
113
|
+
|
114
|
+
def value_options(key)
|
115
|
+
value = attribute_value.try(:[], key)
|
116
|
+
opts = {}
|
117
|
+
opts.merge!({ disabled: 'disabled' }) if key == :sample_shi_hash
|
118
|
+
opts.merge!(value ? { value: value } : {})
|
119
|
+
opts.merge( input_options_for(:value_input) )
|
120
|
+
end
|
121
|
+
|
122
|
+
####
|
123
|
+
# Button for removing hash pair
|
124
|
+
def remove_button
|
125
|
+
opts = input_options_for(:remove_button).fetch(:wrapper, {})
|
126
|
+
wrapper_opts = { class: 'col-xs-2' }.merge(opts)
|
127
|
+
|
128
|
+
template.content_tag(:div, wrapper_opts) do
|
129
|
+
template.concat( template.content_tag(:button, *remove_button_options) )
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def remove_button_options
|
134
|
+
opts = input_options_for(:remove_button)
|
135
|
+
[
|
136
|
+
opts.fetch(:label, 'x'),
|
137
|
+
{
|
138
|
+
class: "#{opts.fetch(:class, 'btn btn-default pull-right')} remove_hash_pair"
|
139
|
+
}.merge(opts.exclude_keys(:label, :class, :wrapper))
|
140
|
+
]
|
141
|
+
end
|
142
|
+
|
143
|
+
####
|
144
|
+
# Block for error handling
|
145
|
+
def error_help_block(key)
|
146
|
+
opts = { class: 'col-xs-12 help-block' }.merge(input_options_for(:error))
|
147
|
+
template.content_tag(:span, error(key), opts)
|
148
|
+
end
|
149
|
+
|
150
|
+
####
|
151
|
+
# Error finder, which based on using store_accessor
|
152
|
+
# and setting validation on hash attributs
|
153
|
+
def error(key)
|
154
|
+
if key.is_a? String
|
155
|
+
k = key.to_sym
|
156
|
+
messages = object.errors.messages
|
157
|
+
|
158
|
+
messages.try(:[], k).try(:first)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
def input_options_for(key)
|
163
|
+
opts = input_options.fetch(key, {})
|
164
|
+
opts.is_a?(Hash) ? opts : {}
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
data/simphi.gemspec
CHANGED
@@ -4,22 +4,22 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "simphi"
|
7
|
-
spec.version = "0.0.
|
7
|
+
spec.version = "0.0.3"
|
8
8
|
spec.authors = ["Alex Beznos"]
|
9
9
|
spec.email = ["beznosa@yahoo.com"]
|
10
10
|
spec.summary = %q{Ability to deale with hash like inputs}
|
11
|
-
spec.description = %q{The gem
|
11
|
+
spec.description = %q{The gem for natural using of hashs in web dev}
|
12
12
|
spec.homepage = "https://github.com/AlexBeznos/simphi"
|
13
13
|
spec.license = "MIT"
|
14
14
|
|
15
15
|
spec.files = `git ls-files -z`.split("\x0")
|
16
16
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
-
spec.require_paths = ["lib"]
|
17
|
+
spec.require_paths = ["{lib,vendor}/**/*"]
|
18
18
|
|
19
19
|
spec.add_dependency "simple_form", "> 2.0"
|
20
20
|
spec.add_dependency "rack", "~> 1.6.4"
|
21
|
+
spec.add_dependency "coffee-rails"
|
21
22
|
|
22
23
|
spec.add_development_dependency "bundler", "~> 1.7"
|
23
24
|
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
-
spec.add_development_dependency "rspec"
|
25
25
|
end
|
data/spec/request_spec.rb
CHANGED
@@ -35,7 +35,7 @@ describe Simphi::Request do
|
|
35
35
|
"one" => "one",
|
36
36
|
"two" => {
|
37
37
|
"more" => {
|
38
|
-
"first-
|
38
|
+
"first-simphi" => {
|
39
39
|
"first-pair" => {
|
40
40
|
"key" => "first-key",
|
41
41
|
"value" => "first-value"
|
@@ -47,7 +47,7 @@ describe Simphi::Request do
|
|
47
47
|
}
|
48
48
|
}
|
49
49
|
},
|
50
|
-
"three-
|
50
|
+
"three-simphi" => {
|
51
51
|
"first-pair" => {
|
52
52
|
"key" => "first-key",
|
53
53
|
"value" => "first-value"
|
@@ -84,7 +84,7 @@ describe Simphi::Request do
|
|
84
84
|
context "include only key" do
|
85
85
|
let(:params_before) do
|
86
86
|
{
|
87
|
-
"first-
|
87
|
+
"first-simphi" => {
|
88
88
|
"first-pair" => {
|
89
89
|
"key" => "first-key"
|
90
90
|
}
|
@@ -100,7 +100,7 @@ describe Simphi::Request do
|
|
100
100
|
context "include key and value but with incorect names" do
|
101
101
|
let(:params_before) do
|
102
102
|
{
|
103
|
-
"first-
|
103
|
+
"first-simphi" => {
|
104
104
|
"first-pair" => {
|
105
105
|
"k" => "first-key",
|
106
106
|
"value" => "first-value"
|
@@ -0,0 +1,172 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "simphi"
|
3
|
+
require "simphi/simphi_input"
|
4
|
+
|
5
|
+
RSpec.shared_examples "with add more button" do
|
6
|
+
it "include add more button" do
|
7
|
+
assert_select ".add_hash_pair", 1
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
RSpec.shared_examples "errors block" do
|
12
|
+
it "include certain number of errors" do
|
13
|
+
assert_select ".help-block", count + 1
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
RSpec.shared_examples "include hash elements" do
|
18
|
+
it "include certain number of hash elements with certain number of inputs(key, value for each)" do
|
19
|
+
assert_select hash_element_class, elements_count + 1 do |hash_element|
|
20
|
+
hash_element.each do |element|
|
21
|
+
assert_select element, "input", 2
|
22
|
+
assert_select element, ".remove_hash_pair", 1
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
RSpec.shared_examples "include sample element" do
|
29
|
+
it "include certain number of hash elements with certain number of inputs(key, value for each)" do
|
30
|
+
assert_select "#{hash_element_class}.sample", 1 do |hash_element|
|
31
|
+
hash_element.each do |element|
|
32
|
+
assert_select element, "input", 2
|
33
|
+
assert_select element, ".remove_hash_pair", 1
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
describe "Simphi::SimphiInput", type: :input do
|
41
|
+
let(:klass_name) { "RandomRecord" }
|
42
|
+
let(:attr) { :fake_attr }
|
43
|
+
|
44
|
+
let(:am) { double("ActiveModel", param_key: klass_name) }
|
45
|
+
let!(:record) { double(klass_name, model_name: am, to_key: [1]) }
|
46
|
+
|
47
|
+
let(:hash_element_class) { ".hash-pair" }
|
48
|
+
|
49
|
+
context "with empty hash" do
|
50
|
+
before do
|
51
|
+
allow(record).to receive(attr)
|
52
|
+
allow_any_instance_of(Simphi::SimphiInput).to receive(:error).and_return(nil)
|
53
|
+
|
54
|
+
concat input_for(record, attr, options)
|
55
|
+
end
|
56
|
+
|
57
|
+
context "and without options" do
|
58
|
+
let(:options) { { as: :simphi } }
|
59
|
+
|
60
|
+
it "not include hash elements" do
|
61
|
+
assert_select hash_element_class, 1
|
62
|
+
end
|
63
|
+
|
64
|
+
include_examples "with add more button"
|
65
|
+
include_examples "include sample element"
|
66
|
+
include_examples "errors block" do
|
67
|
+
let(:count) { 0 }
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context "and with required fields" do
|
72
|
+
let(:req_fields) { [:param_1, :param_2] }
|
73
|
+
let(:options) do
|
74
|
+
{
|
75
|
+
as: :simphi,
|
76
|
+
required_fields: req_fields
|
77
|
+
}
|
78
|
+
end
|
79
|
+
|
80
|
+
include_examples "include hash elements" do
|
81
|
+
let(:elements_count) { 2 }
|
82
|
+
end
|
83
|
+
|
84
|
+
include_examples "with add more button"
|
85
|
+
include_examples "include sample element"
|
86
|
+
include_examples "errors block" do
|
87
|
+
let(:count) { 2 }
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
context "with full hash" do
|
93
|
+
before do
|
94
|
+
allow(record).to receive(attr) do
|
95
|
+
{
|
96
|
+
param_1: "first param",
|
97
|
+
param_2: "second param"
|
98
|
+
}
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
context "and without options" do
|
103
|
+
let(:options) { { as: :simphi } }
|
104
|
+
|
105
|
+
before do
|
106
|
+
allow_any_instance_of(Simphi::SimphiInput).to receive(:error).and_return(nil)
|
107
|
+
concat input_for(record, attr, options)
|
108
|
+
end
|
109
|
+
|
110
|
+
include_examples "with add more button"
|
111
|
+
include_examples "include sample element"
|
112
|
+
include_examples "include hash elements" do
|
113
|
+
let(:elements_count) { 2 }
|
114
|
+
end
|
115
|
+
include_examples "errors block" do
|
116
|
+
let(:count) { 2 }
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
context "and without options but with errors" do
|
121
|
+
let(:options) { { as: :simphi } }
|
122
|
+
let(:message) { "message" }
|
123
|
+
|
124
|
+
before do
|
125
|
+
allow(record).to receive(attr).and_return(param_1: "first param")
|
126
|
+
allow_any_instance_of(Simphi::SimphiInput).to receive(:error).and_return(message)
|
127
|
+
concat input_for(record, attr, options)
|
128
|
+
end
|
129
|
+
|
130
|
+
include_examples "include sample element"
|
131
|
+
include_examples "with add more button"
|
132
|
+
include_examples "errors block" do
|
133
|
+
let(:count) { 1 }
|
134
|
+
end
|
135
|
+
include_examples "include hash elements" do
|
136
|
+
let(:elements_count) { 1 }
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
context "with options" do
|
142
|
+
possible_attributes = %i( key_input value_input remove_button error )
|
143
|
+
let(:fake_attr_name) { "fake_attr_name" }
|
144
|
+
|
145
|
+
before do
|
146
|
+
allow(record).to receive(attr).and_return(param_2: "second param")
|
147
|
+
allow_any_instance_of(Simphi::SimphiInput).to receive(:error).and_return(nil)
|
148
|
+
|
149
|
+
concat input_for(record, attr, options)
|
150
|
+
end
|
151
|
+
|
152
|
+
context "class" do
|
153
|
+
possible_attributes.each do |attribute|
|
154
|
+
let(:options) { { as: :simphi, attribute => { class: fake_attr_name } } }
|
155
|
+
|
156
|
+
it "applies" do
|
157
|
+
assert_select [".", fake_attr_name].join, 2
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
context "id" do
|
163
|
+
possible_attributes.each do |attribute|
|
164
|
+
let(:options) { { as: :simphi, attribute => { id: fake_attr_name } } }
|
165
|
+
|
166
|
+
it "applies" do
|
167
|
+
assert_select ["#", fake_attr_name].join, 2
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,8 +1,15 @@
|
|
1
|
-
require "
|
2
|
-
require "
|
1
|
+
require "rails/all"
|
2
|
+
require "nulldb_rspec"
|
3
3
|
require "active_support"
|
4
4
|
require "active_support/core_ext"
|
5
|
-
|
5
|
+
require "rack/test"
|
6
|
+
require "rspec/rails"
|
7
|
+
# require "rails/test_help"
|
8
|
+
|
9
|
+
include NullDB::RSpec::NullifiedDatabase
|
10
|
+
|
11
|
+
ActiveRecord::Base.establish_connection :adapter => :nulldb
|
12
|
+
Dir["#{File.dirname(__FILE__)}/support/*.rb"].each { |f| require f }
|
6
13
|
|
7
14
|
RSpec.configure do |config|
|
8
15
|
config.order = "random"
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module InputExampleGroup
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
include RSpec::Rails::HelperExampleGroup
|
5
|
+
|
6
|
+
def input_for(object, attribute_name, options = {})
|
7
|
+
helper.simple_form_for object, url: "" do |f|
|
8
|
+
f.input attribute_name, options
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
RSpec.configure do |config|
|
14
|
+
config.include InputExampleGroup, type: :input
|
15
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
class SimphiInput
|
2
|
+
HASH_ELEMENT_IDENTIFIER: '.hash-pair'
|
3
|
+
|
4
|
+
constructor: (target) ->
|
5
|
+
@target = $(target)
|
6
|
+
@index = @target.siblings(@HASH_ELEMENT_IDENTIFIER).length + 1
|
7
|
+
|
8
|
+
changedHashIndex: (original) ->
|
9
|
+
original.replace(/hash-\d+/, "hash-#{@index}")
|
10
|
+
|
11
|
+
newHashInput: ->
|
12
|
+
instance = this
|
13
|
+
sample = @target.siblings("#{@HASH_ELEMENT_IDENTIFIER}.sample").clone(true)
|
14
|
+
sample.removeClass('sample')
|
15
|
+
sample.attr('class': instance.changedHashIndex(sample.attr('class')))
|
16
|
+
sample.find('input').each ->
|
17
|
+
input = this
|
18
|
+
$(input).removeAttr('disabled')
|
19
|
+
$.each ['name', 'id'], ->
|
20
|
+
currentValue = $(input).attr(this)
|
21
|
+
$(input).attr(this, instance.changedHashIndex(currentValue))
|
22
|
+
|
23
|
+
return sample
|
24
|
+
|
25
|
+
addHashInput: ->
|
26
|
+
newHashInput = this.newHashInput()
|
27
|
+
$(newHashInput).insertBefore(@target)
|
28
|
+
|
29
|
+
removeHashInput: ->
|
30
|
+
@target.parents('.hash-pair').remove()
|
31
|
+
|
32
|
+
$(document).ready ->
|
33
|
+
####
|
34
|
+
## Add one more hash pair
|
35
|
+
$('button.add_hash_pair').click (e) ->
|
36
|
+
e.preventDefault()
|
37
|
+
|
38
|
+
he = new SimphiInput e.currentTarget
|
39
|
+
he.addHashInput()
|
40
|
+
|
41
|
+
####
|
42
|
+
## Remove hash pair
|
43
|
+
$('button.remove_hash_pair').click (e) ->
|
44
|
+
e.preventDefault()
|
45
|
+
|
46
|
+
he = new SimphiInput e.currentTarget
|
47
|
+
he.removeHashInput()
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simphi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Beznos
|
@@ -14,81 +14,81 @@ dependencies:
|
|
14
14
|
name: simple_form
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - '>'
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '2.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - '>'
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '2.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rack
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ~>
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: 1.6.4
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 1.6.4
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: coffee-rails
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - '>='
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
48
|
-
type: :
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: bundler
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ~>
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '1.7'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ~>
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '1.7'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: rake
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - ~>
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
75
|
+
version: '10.0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - ~>
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '0'
|
83
|
-
description: The gem
|
82
|
+
version: '10.0'
|
83
|
+
description: The gem for natural using of hashs in web dev
|
84
84
|
email:
|
85
85
|
- beznosa@yahoo.com
|
86
86
|
executables: []
|
87
87
|
extensions: []
|
88
88
|
extra_rdoc_files: []
|
89
89
|
files:
|
90
|
-
-
|
91
|
-
-
|
90
|
+
- .gitignore
|
91
|
+
- .rspec
|
92
92
|
- Gemfile
|
93
93
|
- LICENSE.txt
|
94
94
|
- README.md
|
@@ -96,11 +96,16 @@ files:
|
|
96
96
|
- lib/simphi.rb
|
97
97
|
- lib/simphi/middleware.rb
|
98
98
|
- lib/simphi/request.rb
|
99
|
+
- lib/simphi/simphi_input.rb
|
99
100
|
- simphi.gemspec
|
100
101
|
- spec/middleware_spec.rb
|
101
102
|
- spec/request_spec.rb
|
103
|
+
- spec/simphi_input_spec.rb
|
102
104
|
- spec/spec_helper.rb
|
105
|
+
- spec/support/input.rb
|
103
106
|
- tasks/rspec.rake
|
107
|
+
- vendor/assets/javascripts/simphi.js.coffee
|
108
|
+
- vendor/assets/stylesheets/simphi.css
|
104
109
|
homepage: https://github.com/AlexBeznos/simphi
|
105
110
|
licenses:
|
106
111
|
- MIT
|
@@ -108,24 +113,26 @@ metadata: {}
|
|
108
113
|
post_install_message:
|
109
114
|
rdoc_options: []
|
110
115
|
require_paths:
|
111
|
-
- lib
|
116
|
+
- '{lib,vendor}/**/*'
|
112
117
|
required_ruby_version: !ruby/object:Gem::Requirement
|
113
118
|
requirements:
|
114
|
-
- -
|
119
|
+
- - '>='
|
115
120
|
- !ruby/object:Gem::Version
|
116
121
|
version: '0'
|
117
122
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
123
|
requirements:
|
119
|
-
- -
|
124
|
+
- - '>='
|
120
125
|
- !ruby/object:Gem::Version
|
121
126
|
version: '0'
|
122
127
|
requirements: []
|
123
128
|
rubyforge_project:
|
124
|
-
rubygems_version: 2.
|
129
|
+
rubygems_version: 2.4.6
|
125
130
|
signing_key:
|
126
131
|
specification_version: 4
|
127
132
|
summary: Ability to deale with hash like inputs
|
128
133
|
test_files:
|
129
134
|
- spec/middleware_spec.rb
|
130
135
|
- spec/request_spec.rb
|
136
|
+
- spec/simphi_input_spec.rb
|
131
137
|
- spec/spec_helper.rb
|
138
|
+
- spec/support/input.rb
|