classnames-rails-view 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 66300520b93ecc59aa4421dd0412d4a907ea7921
4
+ data.tar.gz: aebcc014d62732d5ff8e792e707e80586d01b8d8
5
+ SHA512:
6
+ metadata.gz: 93b15b233c5aeb74e3252cff496cb852d626f3c17502122f32f0b6d340d45677bd942b9373bff5eb1fc39985aa8fd986a590a3c69d4e4ed260db4f24460c0a14
7
+ data.tar.gz: 94b4a4e568a055f41225d7d2ebd28664f314fbeaf327a35191a7232e2130a2c0de2898b79febeed97ebed4cf4f00532339224ce88ad49f29264ea0a6058b5bf2
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017 Masamoto Miyata
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.md ADDED
@@ -0,0 +1,72 @@
1
+ # Classnames::Rails::View
2
+ This is a view helper that dynamically generates class attributes in Rails view, like [JedWatson/classnames](https://github.com/JedWatson/classnames).
3
+
4
+ ## Usage
5
+
6
+ The Helper method is included automatically, so you can use it immediately.
7
+
8
+ ```erb
9
+ <div class="<%= classNames('foo') %>">Hello World</div>
10
+ <%# <div class="foo">Hello World</div> %>
11
+ ```
12
+
13
+ The `classNames` method takes any number of arguments which can be a string or hash.
14
+ The argument `'foo'` is short for `{ foo: true }`. If the value associated with a given key is falsy, that key won't be included in the output.
15
+
16
+ ```js
17
+ classNames('foo', 'bar'); // => 'foo bar'
18
+ classNames('foo', { bar: true }); // => 'foo bar'
19
+ classNames({ 'foo-bar': true }); // => 'foo-bar'
20
+ classNames({ 'foo-bar': false }); // => ''
21
+ classNames({ foo: true }, { bar: true }); // => 'foo bar'
22
+ classNames({ foo: true, bar: true }); // => 'foo bar'
23
+
24
+ // lots of arguments of various types
25
+ classNames('foo', { bar: true, duck: false }, 'baz', { quux: true }); // => 'foo bar baz quux'
26
+
27
+ // other falsy values are just ignored
28
+ classNames(null, false, 'bar', undefined, 0, 1, { baz: null }, ''); // => 'bar 1'
29
+ ```
30
+
31
+ Arrays will be recursively flattened as per the rules above:
32
+
33
+ ```js
34
+ var arr = ['b', { c: true, d: false }];
35
+ classNames('a', arr); // => 'a b c'
36
+ ```
37
+
38
+ ### Change the method name
39
+
40
+ If you want to change the method name, you can change it with config when starting the application.
41
+
42
+ ```rb
43
+ # config/application.rb
44
+ class Application < Rails::Application
45
+ ...
46
+ config.classnames_rails_view.method_name = :classes
47
+ end
48
+ ```
49
+
50
+ ```erb
51
+ <div class="<%= classes('foo') %>">Hello World</div>
52
+ ```
53
+
54
+ ## Installation
55
+ Add this line to your application's Gemfile:
56
+
57
+ ```ruby
58
+ gem 'classnames-rails-view'
59
+ ```
60
+
61
+ And then execute:
62
+ ```bash
63
+ $ bundle
64
+ ```
65
+
66
+ ## Contributing
67
+
68
+ * The test uses Rspec. Please execute the test with `rspec` command.
69
+ * Please code check with [rubocop](https://github.com/bbatsov/rubocop).
70
+
71
+ ## License
72
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,27 @@
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 = 'Classnames::Rails::View'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,9 @@
1
+ module Classnames
2
+ module Rails
3
+ module View
4
+ end
5
+ end
6
+ end
7
+
8
+ require 'classnames/rails/view/core'
9
+ require 'classnames/rails/view/railtie'
@@ -0,0 +1,22 @@
1
+ module Classnames
2
+ module Rails
3
+ module View
4
+ module Helper
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ include Classnames::Rails::View::Helper::LocalInstanceMethods
9
+ end
10
+
11
+ module LocalInstanceMethods
12
+ classnames = Classnames::Rails::View::Core.new
13
+ define_method ::Rails.application.config.classnames_rails_view.method_name do |*args|
14
+ classnames.exec(*args)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ ActionView::Base.send :include, Classnames::Rails::View::Helper
@@ -0,0 +1,34 @@
1
+ module Classnames
2
+ module Rails
3
+ module View
4
+ class Core
5
+ def exec(*args)
6
+ inject_values args
7
+ end
8
+
9
+ private
10
+
11
+ def detect_elem(elem)
12
+ if elem.is_a?(Hash)
13
+ elem.inject '' do |out, (key, value)|
14
+ value ? out << key.to_s << ' ' : out
15
+ end.strip
16
+ elsif elem.is_a?(Array)
17
+ inject_values elem
18
+ elsif elem == false
19
+ ''
20
+ else
21
+ elem.to_s
22
+ end
23
+ end
24
+
25
+ def inject_values(values)
26
+ values.inject '' do |out, value|
27
+ detected = detect_elem(value)
28
+ detected == '' ? out : out << detect_elem(value) << ' '
29
+ end.strip
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,14 @@
1
+ module Classnames
2
+ module Rails
3
+ module View
4
+ class Railtie < ::Rails::Railtie
5
+ config.classnames_rails_view = ActiveSupport::OrderedOptions.new
6
+ config.classnames_rails_view.method_name = :classNames
7
+
8
+ config.after_initialize do |_app|
9
+ require 'classnames/rails/view/action_view'
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ module Classnames
2
+ module Rails
3
+ module View
4
+ VERSION = '0.1.0'.freeze
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :classnames_rails_view do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: classnames-rails-view
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Masamoto Miyata
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-11-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: 5.1.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.1.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry-byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: pry-doc
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-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: pry-stack_explorer
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rails-controller-testing
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
+ - !ruby/object:Gem::Dependency
98
+ name: rspec-rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: sqlite3
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Make methods similar to Javascript 's classnames available in the rails
126
+ erb template.
127
+ email:
128
+ - miyata@sincere-co.com
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - MIT-LICENSE
134
+ - README.md
135
+ - Rakefile
136
+ - lib/classnames/rails/view.rb
137
+ - lib/classnames/rails/view/action_view.rb
138
+ - lib/classnames/rails/view/core.rb
139
+ - lib/classnames/rails/view/railtie.rb
140
+ - lib/classnames/rails/view/version.rb
141
+ - lib/tasks/classnames/rails/view_tasks.rake
142
+ homepage: https://github.com/gomo/classnames-rails-view
143
+ licenses:
144
+ - MIT
145
+ metadata: {}
146
+ post_install_message:
147
+ rdoc_options: []
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ requirements: []
161
+ rubyforge_project:
162
+ rubygems_version: 2.6.14
163
+ signing_key:
164
+ specification_version: 4
165
+ summary: The helper method to dynamically generate class attribute of HTML tag.
166
+ test_files: []