rails_js_helper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ed02fdd14d945ce868164b67d43c7fdc0249475a
4
+ data.tar.gz: eae0ad1fad53ff7d9e6836cca95d73b06497c338
5
+ SHA512:
6
+ metadata.gz: a0035c27a2adc4b8e83b5107c34d3512a34598293663de22470843d5503993c76d99ff0c8947fad275f85b429ec00acd3a6c5ce3f38c2300f1b07d2352c231cc
7
+ data.tar.gz: 7f523506f931890c30a4d0e1d18c9ae596278b758649b77b9b79be23b5d7f4fa3914779722e6c00e39d87aeb0f7938524cfa4e7464b3df98f39e072fe25bed1b
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /log/
11
+
12
+ /spec/dummy/tmp
13
+ /spec/dummy/log
14
+ /spec/dummy/public/assets
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ sudo: false
3
+ rvm:
4
+ - 2.2
5
+
6
+ before_install: gem update bundler
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ source 'https://rails-assets.org' do
4
+ gem 'rails-assets-URIjs'
5
+ end
6
+
7
+ gemspec
@@ -0,0 +1,80 @@
1
+ # RailsJsHelper
2
+ [![Build Status](https://travis-ci.org/joker1007/rails_js_helper.svg)](https://travis-ci.org/joker1007/rails_js_helper)
3
+
4
+ this gem define `RailsHelper` on JS.
5
+ this gem embeds `image_path`, `asset_path` and routing table on compile timing on `rails_helper.js.coffee.erb.`
6
+
7
+ `RailsHelper` has some functions.
8
+
9
+
10
+ ## Limitation
11
+ this gem can use only no segments named routes.
12
+
13
+ ex.
14
+
15
+ ```ruby
16
+ # config/routes.rb
17
+ Rails.application.routes.draw do
18
+ resources :users
19
+ end
20
+ end
21
+ ```
22
+
23
+ can use only `users`, `new_user`.
24
+
25
+ ## Installation
26
+
27
+ Add this line to your application's Gemfile:
28
+
29
+ ```ruby
30
+ gem 'rails_js_helper'
31
+ ```
32
+
33
+ *Caution* this gem requires `rails-assets-URIjs`.
34
+
35
+ And then execute:
36
+
37
+ $ bundle
38
+
39
+ ## Usage
40
+
41
+ ```sh
42
+ $ bundle exec rails g rails_js_helper:config
43
+
44
+ # And edit config/rails_js_helper.yml
45
+ ```
46
+
47
+ ```yml
48
+ images
49
+ - loading.png
50
+ assets:
51
+ - loading.css
52
+ configs:
53
+ google_api_key: <%= Rails.application.config.x.google_api_key %>
54
+ ```
55
+
56
+ ```coffee
57
+ #= require rails_helper
58
+
59
+ RailsHelper.image_path("loading.png") # => "/assets/loading.png"
60
+ RailsHelper.image_tag("loading.png", width: 120) # => <img src="loading.png" width="120">
61
+ RailsHelper.asset_path("users.css") # => /assets/users.css
62
+ RailsHelper.named_route("users", format: "json", foo: "bar") # => "/users.json?foo=bar"
63
+ RailsHelper.named_route("new_users") # => "/users/new"
64
+ RailsHelper.route_names() # show all route names on javascript
65
+ RailsHelper.config.google_api_key # => value of `Rails.application.config.x.google_api_key`
66
+ ```
67
+
68
+ ## Development
69
+
70
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
71
+
72
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
73
+
74
+ ## Contributing
75
+
76
+ 1. Fork it ( https://github.com/joker1007/rails_js_helper/fork )
77
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
78
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
79
+ 4. Push to the branch (`git push origin my-new-feature`)
80
+ 5. Create a new Pull Request
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,63 @@
1
+ #= require URIjs/URI.js
2
+
3
+ @RailsHelper = {
4
+ image_path_table: <%= RailsJsHelper.image_path_table.to_json %>
5
+
6
+ asset_path_table: <%= RailsJsHelper.asset_path_table.to_json %>
7
+
8
+ named_route_table: <%= RailsJsHelper.named_route_table.to_json %>
9
+
10
+ config: <%= RailsJsHelper.config_table.to_json %>
11
+
12
+ imagePath: (path) ->
13
+ RailsHelper.image_path_table[path]
14
+
15
+ assetPath: (path) ->
16
+ RailsHelper.asset_path_table[path]
17
+
18
+ imageTag: (path, attributes = {}) ->
19
+ src = RailsHelper.imagePath(path)
20
+ unless src
21
+ throw new Error("Asset path is nothing on table")
22
+ attrs = []
23
+ for k, v of attributes
24
+ attrs.push("#{k}=\"#{v}\"")
25
+ "<img src=\"#{src}\" #{attrs.join(" ")}>"
26
+
27
+ pathFor: (name, options = {}) ->
28
+ uri = RailsHelper._makeURI(name, options)
29
+ uri.toString()
30
+
31
+ urlFor: (name, options = {}) ->
32
+ protocol = "http"
33
+ host = location.host
34
+
35
+ if options.protocol
36
+ protocol = options.protocol
37
+ delete options.protocol
38
+
39
+ if options.host
40
+ host = options.host
41
+ delete options.host
42
+
43
+ uri = RailsHelper._makeURI(name, options)
44
+ uri.host(host)
45
+ uri.protocol(protocol)
46
+ uri.toString()
47
+
48
+ _makeURI: (name, options = {}) ->
49
+ base = RailsHelper.named_route_table[name]
50
+ unless base
51
+ throw new Error("Routing is nothing on table")
52
+ uri = new URI(base)
53
+ uri.suffix(options.format) if options.format?
54
+ delete options.format
55
+ uri.query(options)
56
+ uri
57
+
58
+ routeNames: ->
59
+ names = []
60
+ for name, _url of RailsHelper.named_route_table
61
+ names.push(name)
62
+ names
63
+ }
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rails_js_helper"
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
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,16 @@
1
+ module RailsJsHelper
2
+ class ConfigGenerator < ::Rails::Generators::Base
3
+ # Copied files come from templates folder
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ # Generator desc
7
+ desc <<-DESC
8
+ Creates a default rails_js_helper.yml configuration file in your app's config
9
+ folder.
10
+ DESC
11
+
12
+ def copy_initializer_file
13
+ copy_file "rails_js_helper.yml", "config/rails_js_helper.yml"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,6 @@
1
+ images:
2
+ # - loading.png
3
+ assets:
4
+ # - sortable.css
5
+ configs:
6
+ # aws_api_key: "abcdefg"
@@ -0,0 +1,53 @@
1
+ require "rails_js_helper/version"
2
+
3
+ require 'active_support/configurable'
4
+
5
+ module RailsJsHelper
6
+ class << self
7
+ def image_path_table
8
+ images = load_config["images"].to_a
9
+ Hash[
10
+ images.map do |path|
11
+ [path, ActionController::Base.helpers.image_path(path)]
12
+ end
13
+ ]
14
+ end
15
+
16
+ def asset_path_table
17
+ assets = load_config["assets"].to_a
18
+ Hash[
19
+ assets.map do |path|
20
+ [path, ActionController::Base.helpers.image_path(path)]
21
+ end
22
+ ]
23
+ end
24
+
25
+ def named_route_table
26
+ Rails.application.routes.named_routes
27
+ .select { |name, route| (route.parts - route.optional_parts).empty? }
28
+ .map {|name, route| [name, Rails.application.routes.url_helpers.send("#{name}_path")] }
29
+ .to_h
30
+ end
31
+
32
+ def config_table
33
+ load_config["configs"] || {}
34
+ end
35
+
36
+ private
37
+
38
+ def load_config
39
+ if config_file.exist?
40
+ YAML.load(ERB.new(config_file.read).result)
41
+ else
42
+ {}
43
+ end
44
+ end
45
+
46
+ def config_file
47
+ Rails.root.join("config", "rails_js_helper.yml")
48
+ end
49
+ end
50
+ end
51
+
52
+ require 'rails'
53
+ require "rails_js_helper/engine"
@@ -0,0 +1,13 @@
1
+ module RailsJsHelper
2
+ class Engine < ::Rails::Engine
3
+ initializer "rails_js_helper-preprocessor", after: "sprockets.environment" do
4
+ Rails.application.assets.register_preprocessor "application/javascript", :"rails_js_helper-dependency" do |context, source|
5
+ if context.logical_path == "rails_helper"
6
+ context.depend_on(Rails.root.join("config", "rails_js_helper.yml").to_path)
7
+ context.depend_on(Rails.root.join("config", "routes.rb").to_path)
8
+ end
9
+ source
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module RailsJsHelper
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails_js_helper/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails_js_helper"
8
+ spec.version = RailsJsHelper::VERSION
9
+ spec.authors = ["joker1007"]
10
+ spec.email = ["kakyoin.hierophant@gmail.com"]
11
+
12
+ spec.summary = %q{Add Rails asset helper and named_route helper for JavaScript}
13
+ spec.description = %q{Add Rails asset helper and named_route helper for JavaScript}
14
+ spec.homepage = "https://github.com/joker1007/rails_js_helper"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "rails", ">= 3.2"
22
+ spec.add_runtime_dependency "rails-assets-URIjs"
23
+ spec.add_runtime_dependency "coffee-rails", ">= 3.2"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.9"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec-rails"
28
+ spec.add_development_dependency "sqlite3"
29
+ spec.add_development_dependency "capybara"
30
+ spec.add_development_dependency "poltergeist"
31
+ end
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_js_helper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - joker1007
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-05-14 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.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails-assets-URIjs
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: coffee-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.9'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-rails
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: sqlite3
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: capybara
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
+ - !ruby/object:Gem::Dependency
126
+ name: poltergeist
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Add Rails asset helper and named_route helper for JavaScript
140
+ email:
141
+ - kakyoin.hierophant@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - ".travis.yml"
149
+ - Gemfile
150
+ - README.md
151
+ - Rakefile
152
+ - app/assets/javascripts/rails_helper.js.coffee.erb
153
+ - bin/console
154
+ - bin/setup
155
+ - lib/rails/generators/rails_js_helper/config/config_generator.rb
156
+ - lib/rails/generators/rails_js_helper/config/templates/rails_js_helper.yml
157
+ - lib/rails_js_helper.rb
158
+ - lib/rails_js_helper/engine.rb
159
+ - lib/rails_js_helper/version.rb
160
+ - rails_js_helper.gemspec
161
+ homepage: https://github.com/joker1007/rails_js_helper
162
+ licenses: []
163
+ metadata: {}
164
+ post_install_message:
165
+ rdoc_options: []
166
+ require_paths:
167
+ - lib
168
+ required_ruby_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ requirements: []
179
+ rubyforge_project:
180
+ rubygems_version: 2.4.6
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: Add Rails asset helper and named_route helper for JavaScript
184
+ test_files: []