get_view_name 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 338b5129a05f07e02d51c60479c3a82ef1f5cc52732ae6a73a4b40410857ca49
4
+ data.tar.gz: bb3b64bf2016dae0c74c6f168eb5d06083bad6597e3ce74fdc5565e0a5b6c382
5
+ SHA512:
6
+ metadata.gz: adbea3c506c88544daf4078ed4ba052c1758aa822d953e3dbd37f353190033604e6ee83370907bd195c569b05c792b8d66d7e6f20659a3c51c42db275e0ac898
7
+ data.tar.gz: 52784549a457d831431a50132e6ff5dbb8351faabb63da9fe51e261dff35069575172b62ad9e0c067eb4e2f64ed27190250e23a8e1548cb3e13c0bc1e0920b52
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in get_view_name.gemspec
6
+ gemspec
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ get_view_name (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ rake (10.5.0)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ bundler (~> 1.16)
16
+ get_view_name!
17
+ rake (~> 10.0)
18
+
19
+ BUNDLED WITH
20
+ 1.16.5
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 muratiger
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,109 @@
1
+ # GetViewName
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'get_view_name'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ ## Usage
16
+
17
+ You can get the name of the view (not the action) to be rendered using the `view_name` method in the view context (which includes layout and parcial).
18
+
19
+ Controller: render index
20
+ ```ruby
21
+ def index
22
+ @samples = Sample.all
23
+ end
24
+ ```
25
+ View:
26
+ ```ruby
27
+ <% @samples.each do |blog| %>
28
+ render partial: 'sample', sample: sample
29
+ <% end %>
30
+
31
+ <%= view_name %> => "index"
32
+ ```
33
+ Parcial:
34
+ ```ruby
35
+ <% @samples.each do |sample| %>
36
+ render partial: 'sample', sample: sample
37
+ <% end %>
38
+
39
+ <%= view_name %> => "index"
40
+ ```
41
+
42
+ Layout:
43
+ ```ruby
44
+ <html>
45
+ <head>
46
+ <title>Sample</title>
47
+ <%= stylesheet_link_tag 'application' %>
48
+ <%= javascript_include_tag 'application' %>
49
+ </head>
50
+ <body>
51
+ <%= yield %>
52
+ </body>
53
+ </html>
54
+
55
+ <%= view_name %> => "index"
56
+ ```
57
+ ## Convenient usage
58
+
59
+ If you want new.html.erb to read css and javascript for new,
60
+
61
+ In the action_name method, create is returned when new is rendered with create action as shown below.
62
+
63
+ In such a case gem get_view_name can get the name of the rendered file, so it is possible to load assets to the view to be rendered.
64
+
65
+ Controller: render new
66
+ ```ruby
67
+ def create
68
+ @sample = Sample.new(sample_params)
69
+
70
+ if @sample.save # return false!
71
+ redirect_to @sample
72
+ else
73
+ render :new
74
+ end
75
+ end
76
+ ```
77
+
78
+ Layout:
79
+ ```ruby
80
+ <html>
81
+ <head>
82
+ <%= stylesheet_link_tag 'application' %>
83
+ <%= javascript_include_tag 'application' %>
84
+
85
+ <% if view_name == 'new' %> # if you use action_name method, it returns 'create'
86
+ <%= stylesheet_link_tag 'new' %>
87
+ <%= javascript_include_tag 'new' %>
88
+ <% end %>
89
+ </head>
90
+
91
+ <body>
92
+ <%= yield %>
93
+ </body>
94
+ </html>
95
+ ```
96
+
97
+ ## Development
98
+
99
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
100
+
101
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
102
+
103
+ ## Contributing
104
+
105
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/get_view_name.
106
+
107
+ ## License
108
+
109
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "get_view_name"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,28 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "get_view_name/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "get_view_name"
8
+ spec.version = GetViewName::VERSION
9
+ spec.authors = ["muratiger"]
10
+ spec.email = ["muratiger2018@gmail.com"]
11
+
12
+ spec.summary = %q{Get rendered view name}
13
+ spec.description = %q{Get rendered view name not action}
14
+ spec.homepage = "https://github.com/muratiger/get_view_name"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.16"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ end
@@ -0,0 +1,17 @@
1
+ require "get_view_name/version"
2
+
3
+ module GetViewName
4
+ def render_template(template, layout_name = nil, locals = nil)
5
+ @view.instance_variable_set(:@view_name, extract_file_name(template.identifier))
6
+ @view.define_singleton_method(:view_name) { instance_variable_get(:@view_name) }
7
+ super(template, layout_name, locals)
8
+ end
9
+
10
+ def extract_file_name(fullpath)
11
+ fullpath.split('/').last.split('.').first
12
+ end
13
+ end
14
+
15
+ ActionView::TemplateRenderer.class_eval do
16
+ prepend GetViewName
17
+ end
@@ -0,0 +1,3 @@
1
+ module GetViewName
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: get_view_name
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - muratiger
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-09-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Get rendered view name not action
42
+ email:
43
+ - muratiger2018@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - Gemfile.lock
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - get_view_name.gemspec
57
+ - lib/get_view_name.rb
58
+ - lib/get_view_name/version.rb
59
+ homepage: https://github.com/muratiger/get_view_name
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.7.7
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Get rendered view name
83
+ test_files: []