disable_section 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MWI2NjBjNTY4MjEyZjZjNDA5MzFlN2E0MTk3MjlkYzYxNDgxNjQwMQ==
5
+ data.tar.gz: !binary |-
6
+ ZjdjMDVhZTE0ZTE4MjQ1ZmVkNTA2MzQyOTcxYjBhNThkMjc5YjcwMA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZmViM2VjYmJhOTc3NzAwYTY5ZmE1ZDBmOTE5NDkyMWQyZDdlYzdmZTVkNTc1
10
+ OWQxNDU2ZjAzYTk5YjVlYzQxNmNjZjk1Nzg1Yjg5OWM0NzJiMDk3ODFjNDUx
11
+ NTBmZjVlYWNlODhlZjliOTk1NjgzMDQ5OTkxYWM4ZjQwOWUyMTg=
12
+ data.tar.gz: !binary |-
13
+ N2FjOTgxYzc4MmIxYzYzMDczZGE2YjQ0NjQ2NDZjNWRkZDI5NjUwMzNjMDFj
14
+ N2RiN2JkNjMzOTY4ODg3YWFhMDBjMTc1M2YyOTk2ZTBlMWUzOGYyODYzM2Qx
15
+ ODNiNTFkMzJlMzVjNjcwNzBlZTZkYzUwZTQ5OTBkZmZiMmJiYTk=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in disable_section.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 yaniv preiss
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # Disable Section
2
+
3
+ UI helper that disables an entire section with its inner html elements based on a given condition during render phase.
4
+ For example, you have a form which can be edited only in "edit mode" but readonly in "view mode".
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'disable_section'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install disable_section
19
+
20
+ ## Usage
21
+
22
+ * In the ```application.js``` add
23
+
24
+ ```
25
+ //= require disable_section
26
+ ```
27
+
28
+ * In your html erb, use the ```disable_section``` helper, which receives a predicate and then a block to render.
29
+
30
+ If the predicate is true, then the default elements: a, input, select, textarea and button will be added the class 'disabled' (which you need to create CSS for), and also be added the 'disabled' attribute.
31
+
32
+ If the predicate is false, then the block will be rendered normally.
33
+
34
+ ```ruby
35
+ <%= disable_section view_mode? do %> <!-- suppose view_mode? is your redicate -->
36
+ <%= f.email_field :email %>
37
+ <% end %>
38
+ ```
39
+
40
+ * If you want specific elements within the section to remain enabled, give them class ```keep-enabled```
41
+
42
+
43
+ ## Notes
44
+
45
+ * Note that it depends on jQuery, and works also on future elements (e.g. add new DOM elements via AJAX).
46
+
47
+ * It doesn't act as a toggler, that it, changing the predicate after the view has been rendered will have no effect.
48
+
49
+ ## TODO
50
+
51
+ Specs - add higher level and integration tests
52
+
53
+ ## License
54
+
55
+ See MIT-LICENSE
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'disable_section/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "disable_section"
8
+ spec.version = DisableSection::VERSION
9
+ spec.authors = ["yaniv preiss"]
10
+ spec.email = ["yanivpr@gmail.com"]
11
+ spec.summary = %q{UI helper that disables an entire section with its inner html elements based on a given condition}
12
+ spec.description = %q{}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "rails", ">= 3.1"
21
+ spec.add_dependency "jquery-rails"
22
+
23
+ spec.add_development_dependency "rspec", "~> 2.6"
24
+ spec.add_development_dependency "rspec-rails"
25
+ spec.add_development_dependency "bundler", "~> 1.4"
26
+ spec.add_development_dependency "rake"
27
+ end
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Created by yanivpreiss on 4/15/14.
3
+ */
4
+
5
+ var DisableSection = DisableSection || {};
6
+ DisableSection.elements = 'a,input,select,textarea,button';
7
+
8
+ DisableSection.disableSections = function () {
9
+ var $elementsToDisable = $(".disabled-section").find(DisableSection.elements).not(".keep-enabled");
10
+ $elementsToDisable.addClass("disabled");
11
+ $elementsToDisable.attr("disabled", "disabled");
12
+ };
13
+
14
+ DisableSection.subscribeDomChanges = function(){
15
+ document.body.addEventListener("DOMNodeInserted", function(){
16
+ DisableSection.disableSections();
17
+ }, false);
18
+ };
19
+
20
+ $(document).ready(function(){
21
+ DisableSection.subscribeDomChanges();
22
+ DisableSection.disableSections();
23
+ });
@@ -0,0 +1,5 @@
1
+ require 'action_view'
2
+ require "disable_section/version"
3
+ require 'disable_section/engine'
4
+ require 'disable_section/view_helpers'
5
+ require 'disable_section/railtie' if defined? Rails
@@ -0,0 +1,8 @@
1
+ require 'disable_section/version'
2
+
3
+ module DisableSection
4
+ module Rails
5
+ class Engine < ::Rails::Engine
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+ require 'disable_section/view_helpers'
2
+
3
+ module DisableSection
4
+ class Railtie < ::Rails::Railtie
5
+ initializer "disable_section.view_helpers" do |app|
6
+ ActionView::Base.send :include, ViewHelpers
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module DisableSection
2
+ VERSION = "0.0.6"
3
+ end
@@ -0,0 +1,14 @@
1
+ module DisableSection
2
+ module ViewHelpers
3
+
4
+ DEFAULT_ELEMENTS = %w(a input select textarea button)
5
+
6
+ def disable_section(should_be_disabled = true, elements = DEFAULT_ELEMENTS, &block)
7
+ content_tag :div,
8
+ { class: (should_be_disabled ? 'disabled-section' : ''),
9
+ data: { elements: elements } },
10
+ &block
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+ require 'action_view/helpers'
3
+ require 'disable_section/view_helpers'
4
+
5
+ describe "DisableSection::ViewHelpers" do
6
+ include DisableSection::ViewHelpers
7
+ include ActionView::Helpers::TagHelper
8
+ include ActionView::Helpers::FormTagHelper
9
+ include ActionView::Context
10
+
11
+ context "disable_section" do
12
+
13
+ it "no block" do
14
+ expected = "<div>#{ERB::Util.html_escape({:class=>'disabled-section', :data=>{:elements=>['a', 'input', 'select', 'textarea', 'button']}})}</div>"
15
+ result = disable_section
16
+
17
+ result.should == expected
18
+ end
19
+
20
+ it "with no predicate and block containing div" do
21
+ expected = "<div class=\"disabled-section\" data-elements=\"#{ERB::Util.html_escape(['a','input','select','textarea','button']).gsub(' ', '')}\"><div>#{ERB::Util.html_escape({:id=>'inner'})}</div></div>"
22
+ result = disable_section do
23
+ content_tag :div, id: 'inner'
24
+ end
25
+
26
+ result.should == expected
27
+ end
28
+
29
+ it "with true predicate and block containing div" do
30
+ expected = "<div class=\"disabled-section\" data-elements=\"#{ERB::Util.html_escape(['a','input','select','textarea','button']).gsub(' ', '')}\"><div>#{ERB::Util.html_escape({:id=>'inner'})}</div></div>"
31
+ predicate = true
32
+ result = disable_section predicate do
33
+ content_tag :div, id: 'inner'
34
+ end
35
+
36
+ result.should == expected
37
+ end
38
+
39
+ it "with false predicare block containing input" do
40
+ expected = "<div class=\"\" data-elements=\"#{ERB::Util.html_escape(['a','input','select','textarea','button']).gsub(' ', '')}\"><input id=\"_:id___inner__\" name=\"#{ERB::Util.html_escape({:id=>'inner'})}\" type=\"text\" /></div>"
41
+ result = disable_section false do
42
+ text_field_tag id: 'inner'
43
+ end
44
+
45
+ result.should == expected
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,33 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'bundler/setup'
4
+ require 'rails'
5
+ require 'rspec'
6
+ require 'disable_section'
7
+
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
13
+
14
+ #ENV["RAILS_ENV"] = "test"
15
+ #require 'bundler/setup'
16
+ #Bundler.setup
17
+ #
18
+ #require 'rails/all'
19
+ ##require 'active_support'
20
+ ##require 'active_support/core_ext'
21
+ #require 'rspec/rails'
22
+ #require 'action_view'
23
+ #require 'action_view/helpers'
24
+ #require 'action_view/base'
25
+ #require 'action_view'
26
+ #require 'rspec/rails/view_rendering'
27
+ #require 'rspec/core/formatters/html_formatter'
28
+ #require 'rspec/rails/example/view_example_group'
29
+ #require 'disable_section'
30
+ #
31
+ #RSpec.configure do |config|
32
+ # config.include DisableSection::DisableSectionHelper
33
+ #end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: disable_section
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.6
5
+ platform: ruby
6
+ authors:
7
+ - yaniv preiss
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-19 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: '3.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: jquery-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
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: ''
98
+ email:
99
+ - yanivpr@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - disable_section.gemspec
110
+ - lib/assets/javascripts/disable_section.js
111
+ - lib/disable_section.rb
112
+ - lib/disable_section/engine.rb
113
+ - lib/disable_section/railtie.rb
114
+ - lib/disable_section/version.rb
115
+ - lib/disable_section/view_helpers.rb
116
+ - spec/lib/disable_section_helper_spec.rb
117
+ - spec/spec_helper.rb
118
+ homepage: ''
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ! '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ! '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.2.2
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: UI helper that disables an entire section with its inner html elements based
142
+ on a given condition
143
+ test_files:
144
+ - spec/lib/disable_section_helper_spec.rb
145
+ - spec/spec_helper.rb