dom_for 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +25 -0
- data/.travis.yml +6 -0
- data/.yardopts +7 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +21 -0
- data/Gemfile.lock +133 -0
- data/MIT-LICENSE +20 -0
- data/README.md +133 -0
- data/Rakefile +16 -0
- data/dom_for.gemspec +29 -0
- data/lib/dom_for/model.rb +88 -0
- data/lib/dom_for/record.rb +49 -0
- data/lib/dom_for/version.rb +3 -0
- data/lib/dom_for.rb +43 -0
- data/spec/dummy/.rspec +2 -0
- data/spec/dummy/README.rdoc +261 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/assets/javascripts/application.js +15 -0
- data/spec/dummy/app/assets/javascripts/users.js +2 -0
- data/spec/dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/dummy/app/assets/stylesheets/scaffold.css +56 -0
- data/spec/dummy/app/assets/stylesheets/users.css +4 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/controllers/users_controller.rb +83 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/helpers/users_helper.rb +2 -0
- data/spec/dummy/app/mailers/.gitkeep +0 -0
- data/spec/dummy/app/models/.gitkeep +0 -0
- data/spec/dummy/app/models/user.rb +3 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/app/views/users/_form.html.erb +21 -0
- data/spec/dummy/app/views/users/edit.html.erb +6 -0
- data/spec/dummy/app/views/users/index.html.erb +23 -0
- data/spec/dummy/app/views/users/new.html.erb +5 -0
- data/spec/dummy/app/views/users/show.html.erb +10 -0
- data/spec/dummy/config/application.rb +65 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +37 -0
- data/spec/dummy/config/environments/production.rb +67 -0
- data/spec/dummy/config/environments/test.rb +37 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +15 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +61 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/db/migrate/20140316142523_create_users.rb +9 -0
- data/spec/dummy/db/schema.rb +22 -0
- data/spec/dummy/lib/assets/.gitkeep +0 -0
- data/spec/dummy/log/.gitkeep +0 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +25 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/helpers/dom_for_model_spec.rb +57 -0
- data/spec/helpers/dom_for_record_spec.rb +37 -0
- data/spec/helpers/dom_for_spec.rb +15 -0
- data/spec/spec_helper.rb +48 -0
- metadata +247 -0
data/.gitignore
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
.coveralls.yml
|
2
|
+
.idea/
|
3
|
+
vendor/
|
4
|
+
*.gem
|
5
|
+
*.rbc
|
6
|
+
.bundle
|
7
|
+
.config
|
8
|
+
.yardoc
|
9
|
+
Gemfile.lock
|
10
|
+
InstalledFiles
|
11
|
+
_yardoc
|
12
|
+
coverage
|
13
|
+
doc/
|
14
|
+
lib/bundler/man
|
15
|
+
pkg
|
16
|
+
rdoc
|
17
|
+
spec/reports
|
18
|
+
test/tmp
|
19
|
+
test/version_tmp
|
20
|
+
tmp
|
21
|
+
log/*.log
|
22
|
+
spec/dummy/db/*.sqlite3
|
23
|
+
spec/dummy/log/*.log
|
24
|
+
spec/dummy/tmp/
|
25
|
+
spec/dummy/.sass-cache
|
data/.travis.yml
ADDED
data/.yardopts
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
ruby '1.9.3'
|
4
|
+
|
5
|
+
# Declare your gem's dependencies in dom_for.gemspec.
|
6
|
+
# Bundler will treat runtime dependencies like base dependencies, and
|
7
|
+
# development dependencies will be added by default to the :development group.
|
8
|
+
gemspec
|
9
|
+
|
10
|
+
# jquery-rails is used by the dummy application
|
11
|
+
gem 'jquery-rails'
|
12
|
+
|
13
|
+
# Declare any dependencies that are still in development here instead of in
|
14
|
+
# your gemspec. These might include edge Rails or gems from your path or
|
15
|
+
# Git. Remember to move these dependencies to your gemspec before releasing
|
16
|
+
# your gem to rubygems.org.
|
17
|
+
|
18
|
+
# To use debugger
|
19
|
+
# gem 'debugger'
|
20
|
+
|
21
|
+
gem 'coveralls', require: false
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
dom_for (1.0.0)
|
5
|
+
rails (~> 3.2.17)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actionmailer (3.2.17)
|
11
|
+
actionpack (= 3.2.17)
|
12
|
+
mail (~> 2.5.4)
|
13
|
+
actionpack (3.2.17)
|
14
|
+
activemodel (= 3.2.17)
|
15
|
+
activesupport (= 3.2.17)
|
16
|
+
builder (~> 3.0.0)
|
17
|
+
erubis (~> 2.7.0)
|
18
|
+
journey (~> 1.0.4)
|
19
|
+
rack (~> 1.4.5)
|
20
|
+
rack-cache (~> 1.2)
|
21
|
+
rack-test (~> 0.6.1)
|
22
|
+
sprockets (~> 2.2.1)
|
23
|
+
activemodel (3.2.17)
|
24
|
+
activesupport (= 3.2.17)
|
25
|
+
builder (~> 3.0.0)
|
26
|
+
activerecord (3.2.17)
|
27
|
+
activemodel (= 3.2.17)
|
28
|
+
activesupport (= 3.2.17)
|
29
|
+
arel (~> 3.0.2)
|
30
|
+
tzinfo (~> 0.3.29)
|
31
|
+
activeresource (3.2.17)
|
32
|
+
activemodel (= 3.2.17)
|
33
|
+
activesupport (= 3.2.17)
|
34
|
+
activesupport (3.2.17)
|
35
|
+
i18n (~> 0.6, >= 0.6.4)
|
36
|
+
multi_json (~> 1.0)
|
37
|
+
arel (3.0.3)
|
38
|
+
builder (3.0.4)
|
39
|
+
coveralls (0.7.0)
|
40
|
+
multi_json (~> 1.3)
|
41
|
+
rest-client
|
42
|
+
simplecov (>= 0.7)
|
43
|
+
term-ansicolor
|
44
|
+
thor
|
45
|
+
diff-lcs (1.2.5)
|
46
|
+
docile (1.1.3)
|
47
|
+
erubis (2.7.0)
|
48
|
+
hike (1.2.3)
|
49
|
+
i18n (0.6.9)
|
50
|
+
journey (1.0.4)
|
51
|
+
jquery-rails (3.1.0)
|
52
|
+
railties (>= 3.0, < 5.0)
|
53
|
+
thor (>= 0.14, < 2.0)
|
54
|
+
json (1.8.1)
|
55
|
+
mail (2.5.4)
|
56
|
+
mime-types (~> 1.16)
|
57
|
+
treetop (~> 1.4.8)
|
58
|
+
mime-types (1.25.1)
|
59
|
+
multi_json (1.9.0)
|
60
|
+
polyglot (0.3.4)
|
61
|
+
rack (1.4.5)
|
62
|
+
rack-cache (1.2)
|
63
|
+
rack (>= 0.4)
|
64
|
+
rack-ssl (1.3.3)
|
65
|
+
rack
|
66
|
+
rack-test (0.6.2)
|
67
|
+
rack (>= 1.0)
|
68
|
+
rails (3.2.17)
|
69
|
+
actionmailer (= 3.2.17)
|
70
|
+
actionpack (= 3.2.17)
|
71
|
+
activerecord (= 3.2.17)
|
72
|
+
activeresource (= 3.2.17)
|
73
|
+
activesupport (= 3.2.17)
|
74
|
+
bundler (~> 1.0)
|
75
|
+
railties (= 3.2.17)
|
76
|
+
railties (3.2.17)
|
77
|
+
actionpack (= 3.2.17)
|
78
|
+
activesupport (= 3.2.17)
|
79
|
+
rack-ssl (~> 1.3.2)
|
80
|
+
rake (>= 0.8.7)
|
81
|
+
rdoc (~> 3.4)
|
82
|
+
thor (>= 0.14.6, < 2.0)
|
83
|
+
rake (10.1.1)
|
84
|
+
rdoc (3.12.2)
|
85
|
+
json (~> 1.4)
|
86
|
+
redcarpet (3.1.1)
|
87
|
+
rest-client (1.6.7)
|
88
|
+
mime-types (>= 1.16)
|
89
|
+
rspec-core (2.14.8)
|
90
|
+
rspec-expectations (2.14.5)
|
91
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
92
|
+
rspec-mocks (2.14.6)
|
93
|
+
rspec-rails (2.14.1)
|
94
|
+
actionpack (>= 3.0)
|
95
|
+
activemodel (>= 3.0)
|
96
|
+
activesupport (>= 3.0)
|
97
|
+
railties (>= 3.0)
|
98
|
+
rspec-core (~> 2.14.0)
|
99
|
+
rspec-expectations (~> 2.14.0)
|
100
|
+
rspec-mocks (~> 2.14.0)
|
101
|
+
simplecov (0.8.2)
|
102
|
+
docile (~> 1.1.0)
|
103
|
+
multi_json
|
104
|
+
simplecov-html (~> 0.8.0)
|
105
|
+
simplecov-html (0.8.0)
|
106
|
+
sprockets (2.2.2)
|
107
|
+
hike (~> 1.2)
|
108
|
+
multi_json (~> 1.0)
|
109
|
+
rack (~> 1.0)
|
110
|
+
tilt (~> 1.1, != 1.3.0)
|
111
|
+
sqlite3 (1.3.9)
|
112
|
+
term-ansicolor (1.3.0)
|
113
|
+
tins (~> 1.0)
|
114
|
+
thor (0.18.1)
|
115
|
+
tilt (1.4.1)
|
116
|
+
tins (1.0.0)
|
117
|
+
treetop (1.4.15)
|
118
|
+
polyglot
|
119
|
+
polyglot (>= 0.3.1)
|
120
|
+
tzinfo (0.3.39)
|
121
|
+
yard (0.8.7.3)
|
122
|
+
|
123
|
+
PLATFORMS
|
124
|
+
ruby
|
125
|
+
|
126
|
+
DEPENDENCIES
|
127
|
+
coveralls
|
128
|
+
dom_for!
|
129
|
+
jquery-rails
|
130
|
+
redcarpet (~> 3.0)
|
131
|
+
rspec-rails
|
132
|
+
sqlite3
|
133
|
+
yard (~> 0.8.7)
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2014 Mikhail Grachev
|
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,133 @@
|
|
1
|
+
# DomFor
|
2
|
+
|
3
|
+
Helper for creating HTML wrappers ActiveRecord objects
|
4
|
+
|
5
|
+
[![Build Status](https://travis-ci.org/mgrachev/dom_for.png?branch=master)](https://travis-ci.org/mgrachev/dom_for)
|
6
|
+
[![Coverage Status](https://coveralls.io/repos/mgrachev/dom_for/badge.png?branch=master)](https://coveralls.io/r/mgrachev/dom_for?branch=master)
|
7
|
+
[![Dependency Status](https://gemnasium.com/mgrachev/dom_for.svg)](https://gemnasium.com/mgrachev/dom_for)
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'dom_for'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install dom_for
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```erb
|
26
|
+
<%= dom_for User, attribute_1: 'value_1', attribute_2: 'value_2', attribute_3: 'value_3' do %>
|
27
|
+
<% @users.each do |user| %>
|
28
|
+
<%= dom_for user, admin: user.admin, blocked: user.blocked do %>
|
29
|
+
<%= content_tag(:span, user.name) %>
|
30
|
+
<% end %>
|
31
|
+
<% end %>
|
32
|
+
<% end %>
|
33
|
+
```
|
34
|
+
|
35
|
+
```html
|
36
|
+
<div class="users" data-action="index" data-attribute-1="value_1" data-attribute-2="value_2" data-attribute-3="value_3" id="users">
|
37
|
+
<div class="user" data-admin="true" data-blocked="false" data-object-id="1" id="user_1">
|
38
|
+
<span>Mikhail</span>
|
39
|
+
</div>
|
40
|
+
<div class="user" data-admin="false" data-blocked="false" data-object-id="2" id="user_2">
|
41
|
+
<span>Yulia</span>
|
42
|
+
</div>
|
43
|
+
</div>
|
44
|
+
```
|
45
|
+
|
46
|
+
The first argument can be used as a model ActiveRecord:
|
47
|
+
|
48
|
+
```erb
|
49
|
+
<%= dom_for User do %>
|
50
|
+
<%= tag(:span) %>
|
51
|
+
<% end %>
|
52
|
+
```
|
53
|
+
|
54
|
+
```html
|
55
|
+
<div class="users" data-action="index" id="users" />
|
56
|
+
<span />
|
57
|
+
</div>
|
58
|
+
```
|
59
|
+
|
60
|
+
And record ActiveRecord:
|
61
|
+
|
62
|
+
```erb
|
63
|
+
<%= dom_for @user do %>
|
64
|
+
<%= tag(:span) %>
|
65
|
+
<% end %>
|
66
|
+
```
|
67
|
+
|
68
|
+
```html
|
69
|
+
<div class="user" data-action="show" data-object-id="1" id="user_1" />
|
70
|
+
<span />
|
71
|
+
</div>
|
72
|
+
```
|
73
|
+
|
74
|
+
The second argument passed to additional html-attributes (is optional):
|
75
|
+
|
76
|
+
```erb
|
77
|
+
<%= dom_for User, attribute_1: 'value_1', attribute_2: 'value_2' do %>
|
78
|
+
<%= tag(:span) %>
|
79
|
+
<% end %>
|
80
|
+
```
|
81
|
+
|
82
|
+
```html
|
83
|
+
<div class="users" data-action="index" data-attribute-1="value_1" data-attribute-2="value_2" id="users" />
|
84
|
+
<span />
|
85
|
+
</div>
|
86
|
+
```
|
87
|
+
|
88
|
+
The third argument, the helper `dom_for`, takes a block of code that will be wrapped in the tag `<div>` (is optional):
|
89
|
+
|
90
|
+
```erb
|
91
|
+
<%= dom_for User %>
|
92
|
+
```
|
93
|
+
|
94
|
+
```html
|
95
|
+
<div class="users" data-action="index" id="users" />
|
96
|
+
```
|
97
|
+
|
98
|
+
When defined instance variable with class name, the helper `dom_for` creates the additional html-attributes for this object:
|
99
|
+
|
100
|
+
```erb
|
101
|
+
<% @user = User.last %>
|
102
|
+
<%= dom_for User do %>
|
103
|
+
<%= tag(:span) %>
|
104
|
+
<% end %>
|
105
|
+
```
|
106
|
+
|
107
|
+
```html
|
108
|
+
<div class="user" data-action="show" data-object-id="1" data-object="users" id="user_1" >
|
109
|
+
<span />
|
110
|
+
</div>
|
111
|
+
```
|
112
|
+
|
113
|
+
For each request, the helper `dom_for` creates additional attribute `data-action`, which will be equal to the method of the controller handling the request:
|
114
|
+
|
115
|
+
```erb
|
116
|
+
<%= dom_for User do %>
|
117
|
+
<%= tag(:span) %>
|
118
|
+
<% end %>
|
119
|
+
```
|
120
|
+
|
121
|
+
```html
|
122
|
+
<div class="users" data-action="index" id="users">
|
123
|
+
<span />
|
124
|
+
</div>
|
125
|
+
```
|
126
|
+
|
127
|
+
## Contributing
|
128
|
+
|
129
|
+
1. Fork it
|
130
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
131
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
132
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
133
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
|
8
|
+
require 'rspec/core/rake_task'
|
9
|
+
require 'yard'
|
10
|
+
|
11
|
+
RSpec::Core::RakeTask.new(:spec)
|
12
|
+
task :default => :spec
|
13
|
+
|
14
|
+
YARD::Rake::YardocTask.new
|
15
|
+
|
16
|
+
Bundler::GemHelper.install_tasks
|
data/dom_for.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
|
4
|
+
# Maintain your gem's version:
|
5
|
+
require 'dom_for/version'
|
6
|
+
|
7
|
+
# Describe your gem and declare its dependencies:
|
8
|
+
Gem::Specification.new do |s|
|
9
|
+
s.name = 'dom_for'
|
10
|
+
s.version = DomFor::VERSION
|
11
|
+
s.authors = ['Mikhail Grachev']
|
12
|
+
s.email = ['work@mgrachev.com']
|
13
|
+
s.homepage = 'https://github.com/mgrachev/dom_for'
|
14
|
+
s.summary = 'Helper for creating HTML wrappers ActiveRecord objects.'
|
15
|
+
s.description = 'Helper for easy creation of HTML wrapper ActiveRecord objects.'
|
16
|
+
s.license = 'MIT'
|
17
|
+
|
18
|
+
s.files = `git ls-files -z`.split("\x0")
|
19
|
+
s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
21
|
+
s.require_paths = ['lib']
|
22
|
+
|
23
|
+
s.add_dependency 'rails', '~> 3.2.17'
|
24
|
+
|
25
|
+
s.add_development_dependency 'sqlite3'
|
26
|
+
s.add_development_dependency 'rspec-rails'
|
27
|
+
s.add_development_dependency 'yard', '~> 0.8.7'
|
28
|
+
s.add_development_dependency 'redcarpet', '~> 3.0' # Markdown implementation (for yard)
|
29
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
module DomFor
|
2
|
+
#
|
3
|
+
# Name of classes
|
4
|
+
# Pages:
|
5
|
+
# index - users
|
6
|
+
# new - user new_user
|
7
|
+
# edit - user edit_user
|
8
|
+
# show - user show_user
|
9
|
+
#
|
10
|
+
# ID name
|
11
|
+
# Pages:
|
12
|
+
# index - users
|
13
|
+
# new - new_user
|
14
|
+
# edit - user_1
|
15
|
+
# show - user_1
|
16
|
+
#
|
17
|
+
# Data-attributes
|
18
|
+
# Pages:
|
19
|
+
# index - data-action = index
|
20
|
+
# new - data-action = new
|
21
|
+
# edit - data-action = edit, data-object-id = 1
|
22
|
+
# show - data-action = show, data-object-id = 1
|
23
|
+
#
|
24
|
+
module Model
|
25
|
+
#
|
26
|
+
# Creates a div tag with the attributes for the model of ActiveRecord
|
27
|
+
#
|
28
|
+
# @example Without the block:
|
29
|
+
# dom_for_model(User) #=> <div class="users" id="users" />
|
30
|
+
#
|
31
|
+
# @example When there is a request:
|
32
|
+
# dom_for_model(User) #=> <div class="users" data-action="show" id="users" />
|
33
|
+
#
|
34
|
+
# @example For the new user:
|
35
|
+
# dom_for_model(User) #=> <div class="user new_user" data-action="new" id="new_user" />
|
36
|
+
#
|
37
|
+
# @example For the saved record (if exists @user):
|
38
|
+
# dom_for_model(User) #=> <div class="user show_user" data-action="show" data-object-id="1" data-object="users" id="user_1" />
|
39
|
+
#
|
40
|
+
# @example With the additional attributes:
|
41
|
+
# dom_for_model(User, admin: true) #=> <div class="user show_user" data-action="show" data-admin="true" data-object-id="1" data-object="users" id="user_1" />
|
42
|
+
#
|
43
|
+
# @example With the block:
|
44
|
+
# dom_for_model(User, admin: true) do
|
45
|
+
# tag(:span)
|
46
|
+
# end #=> <div class="user show_user" data-action="show" data-admin="true" data-object-id="1" data-object="users" id="user_1"><span /></div>
|
47
|
+
#
|
48
|
+
# @param [Class] klass Model of ActiveRecord::Base
|
49
|
+
# @param [Hash] attrs Additional attributes for the record
|
50
|
+
# @param [Proc] block Block for a div tag
|
51
|
+
#
|
52
|
+
# @return [String] Sanitized HTML string
|
53
|
+
#
|
54
|
+
def dom_for_model(klass, attrs={}, &block)
|
55
|
+
object_classes = []
|
56
|
+
class_name = klass.to_s.underscore
|
57
|
+
request_action = request.path_parameters[:action]
|
58
|
+
|
59
|
+
attrs.merge!(action: request_action) if request_action.present?
|
60
|
+
|
61
|
+
object = instance_variable_get("@#{class_name}")
|
62
|
+
|
63
|
+
object_id = if object
|
64
|
+
if object.persisted?
|
65
|
+
attrs = attrs.merge(object_id: object.id, object: class_name.pluralize)
|
66
|
+
end
|
67
|
+
|
68
|
+
object_classes << dom_class(klass)
|
69
|
+
object_classes << dom_class(klass, request_action) if request_action.present?
|
70
|
+
|
71
|
+
dom_id(object)
|
72
|
+
else
|
73
|
+
object_classes << class_name.pluralize
|
74
|
+
|
75
|
+
class_name.pluralize
|
76
|
+
end
|
77
|
+
|
78
|
+
if block_given?
|
79
|
+
content_tag(:div, id: object_id, class: object_classes.join(' '), data: attrs, &block)
|
80
|
+
else
|
81
|
+
tag(:div, id: object_id, class: object_classes.join(' '), data: attrs)
|
82
|
+
end
|
83
|
+
|
84
|
+
rescue
|
85
|
+
content_tag(:div, &block)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module DomFor
|
2
|
+
#
|
3
|
+
# Class name: user
|
4
|
+
#
|
5
|
+
# ID name: user_1
|
6
|
+
#
|
7
|
+
# Data-attributes: data-object-id = 1
|
8
|
+
#
|
9
|
+
module Record
|
10
|
+
#
|
11
|
+
# Creates a div tag with the attributes for the instance of ActiveRecord
|
12
|
+
#
|
13
|
+
# @example For the new record:
|
14
|
+
# dom_for_record(User.new) #=> <div class="user" id="new_user" />
|
15
|
+
#
|
16
|
+
# @example For the saved record:
|
17
|
+
# dom_for_record(@user) #=> <div class="user" data-object-id="1" id="user_1" />
|
18
|
+
#
|
19
|
+
# @example With the additional attributes:
|
20
|
+
# dom_for_record(@user, admin: true) #=> <div class="user" data-admin="true" data-object-id="1" id="user_1" />
|
21
|
+
#
|
22
|
+
# @example With the block:
|
23
|
+
# dom_for_record(@user, admin: true) do
|
24
|
+
# tag(:span)
|
25
|
+
# end #=> <div class="user" data-admin="true" data-object-id="1" id="user_1"><span /></div>
|
26
|
+
#
|
27
|
+
# @param [ActiveRecord::Base] record Instance of ActiveRecord::Base
|
28
|
+
# @param [Hash] attrs Additional attributes for the record
|
29
|
+
# @param [Proc] block Block for a div tag
|
30
|
+
#
|
31
|
+
# @return [String] Sanitized HTML string
|
32
|
+
#
|
33
|
+
def dom_for_record(record, attrs={}, &block)
|
34
|
+
object_id = dom_id(record)
|
35
|
+
object_class = dom_class(record.class)
|
36
|
+
|
37
|
+
attrs = attrs.merge(object_id: record.id) if record.persisted?
|
38
|
+
|
39
|
+
if block_given?
|
40
|
+
content_tag(:div, id: object_id, class: object_class, data: attrs, &block)
|
41
|
+
else
|
42
|
+
tag(:div, id: object_id, class: object_class, data: attrs)
|
43
|
+
end
|
44
|
+
|
45
|
+
rescue
|
46
|
+
content_tag(:div, &block)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/dom_for.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'active_support/dependencies/autoload'
|
2
|
+
|
3
|
+
module DomFor
|
4
|
+
#
|
5
|
+
# dom_for Project do
|
6
|
+
#
|
7
|
+
# dom_for Task do
|
8
|
+
# dom_for task, attribute: 'yes'
|
9
|
+
#
|
10
|
+
# dom_for User do
|
11
|
+
# dom_for user, admin: false
|
12
|
+
#
|
13
|
+
# dom_for Comment do
|
14
|
+
# dom_for comment, private: true
|
15
|
+
#
|
16
|
+
extend ActiveSupport::Autoload
|
17
|
+
|
18
|
+
autoload :Model
|
19
|
+
autoload :Record
|
20
|
+
|
21
|
+
include Model
|
22
|
+
include Record
|
23
|
+
|
24
|
+
#
|
25
|
+
# Creates a div tag with the attributes for the model or record of ActiveRecord
|
26
|
+
#
|
27
|
+
# @param [ActiveRecord::Base, Class] object Model or record of ActiveRecord
|
28
|
+
# @param [Hash] attrs Additional attributes for the record
|
29
|
+
# @param [Proc] block Block for a div tag
|
30
|
+
#
|
31
|
+
# @return [String] Sanitized HTML string
|
32
|
+
#
|
33
|
+
def dom_for(object, attrs={}, &block)
|
34
|
+
if object.instance_of? Class
|
35
|
+
dom_for_model(object, attrs, &block)
|
36
|
+
else
|
37
|
+
dom_for_record(object, attrs, &block)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
ActionView::Helpers.send(:include, DomFor)
|
data/spec/dummy/.rspec
ADDED