chartkick-remote 1.0.2

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: 2e2aecd2927354c657db8bd089ea21ae93c195b8
4
+ data.tar.gz: a82b9562e69548bb9ebe01c86dc78d6e946d760c
5
+ SHA512:
6
+ metadata.gz: 132590888dad96644b1409a157b0fb1e616df4f8e73e8c4246fd9f540c18cb3d892136bb08446111a952b8e6d788712ae7d3dda11fcf11fbf0fd9e4738641f2d
7
+ data.tar.gz: 7689dabb3a643727d697ceafff7357ea1faf50fb6276d94e82b967a0404a9ccf331c1a4f8a248c31f27ccc7784cc7a10949cb545d5986ec872feebcfe1359130
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .ruby*
7
+ .idea*
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
4
+ - 2.0.0
5
+ - 1.9.3
6
+ - jruby-19mode # JRuby in 1.9 mode
7
+ # uncomment this line if your project needs to run something other than `rake`:
8
+ # script: bundle exec rspec spec
9
+
10
+ script:
11
+ CODECLIMATE_REPO_TOKEN=25d03499fa4fad3f1edcd9d382e7a8d98a340b313cf9384ca2c679c8bf25d721 bundle exec rake
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in chartkick.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Andrew Brown based on work by Andrew Kane
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.
@@ -0,0 +1,77 @@
1
+ # Chartkick Remote
2
+
3
+ [![Travis CI Status](https://travis-ci.org/dontfidget/chartkick-remote.svg?branch=master)](https://travis-ci.org/dontfidget/chartkick-remote)
4
+
5
+ [![Code Climate](https://codeclimate.com/github/dontfidget/chartkick-remote.png)](https://codeclimate.com/github/dontfidget/chartkick-remote)
6
+
7
+ ## Usage
8
+
9
+ This is an add-on to ankane's Chartkick to allow the user to specify chart data in a block and have it automatically sourced remotely to avoid making too many queries in the original page render.
10
+
11
+ For more on the fabulous Chartkick library, see http://ankane.github.io/chartkick/.
12
+
13
+
14
+ In your controller, add the following to tell the controller to respond to json requests for chart data:
15
+
16
+ ```ruby
17
+ include Chartkick::Remote
18
+ chartkick_remote
19
+ ```
20
+
21
+ Then in your views, pass your data as a block:
22
+
23
+ ```ruby
24
+ <%= line_chart { Task.group_by_day(:completed_at).count } %>
25
+ ```
26
+
27
+ To prevent remote requests for a particular chart, set `remote` to `false` in the chart options:
28
+
29
+ ```ruby
30
+ <%= line_chart(remote: false) { Task.group_by_day(:completed_at).count } %>
31
+ ```
32
+
33
+ This will generate the data set immediately when the page is rendered.
34
+
35
+
36
+ You can also pass arguments to chartkick for all views on your controller as options for the `chartkick_remote` call:
37
+
38
+ ```ruby
39
+ <%= chartkick_remote height: "500px" %>
40
+ ```
41
+
42
+ Finally, if you need to, you can restrict chartkick_remote to particular views:
43
+
44
+ ```ruby
45
+ <%= chartkick_remote only: :show %>
46
+ ```
47
+
48
+ This will prevent the remote requests for other views.
49
+
50
+ ## Installation
51
+
52
+ First, set up 'chartkick' as described at http://ankane.github.io/chartkick/.
53
+
54
+ Then, add this line to your application's Gemfile:
55
+
56
+ ```
57
+ gem 'chartkick-remote'
58
+ ```
59
+
60
+ ## How it Works
61
+
62
+ This gem works by *not* executing "remote" blocks when initially rendering your html page or when making any json request other than the single request that actually needs the data generated in that block. When responding to a json request, the controller will actually render your template, but only to a string that it will discard, and only so that it can find and execute the one block that is generating the data for this json request. This means that:
63
+
64
+ * You can't use results from any code included in any prior "remote" blocks.
65
+ * You should try to do minimal work outside of your block, so that it doesn't get executed unnecessarily for each json request. Specifically, if you define partial results that you will use in your block, make sure that they are lazily evaluated.
66
+
67
+ ## Handling *Many* Requests at Once
68
+
69
+ Using this methodology, it's easy to write a page that makes many, many json requests, which may swamp your server and possibly even time out if you have a global `timeout` value set for your ajax requests. @maccman's jquery.ajax.queue.coffee script provides a basic queueing transport layer for ajax requests which I've modified to provide an option to set the maximum number of requests that can be made in parallel (see https://gist.github.com/dontfidget/1ad9ab33971b64fe6fef). If you include the javascript in this gist on your page, you can then specify the maximum number of allowable requests globally for your page as follows:
70
+
71
+ ```
72
+ $.ajaxSetup({queue: true, queueMaxConcurrency: 2});
73
+ ```
74
+
75
+
76
+
77
+
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'chartkick/remote/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "chartkick-remote"
8
+ spec.version = Chartkick::Remote::VERSION
9
+ spec.authors = ["Andrew S. Brown"]
10
+ spec.email = ["andrew@dontfidget.com"]
11
+ spec.description = %q{Automatically generate remote json for chartkick}
12
+ spec.summary = %q{Automatically generate remote json for chartkick}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest"
24
+ spec.add_development_dependency "chartkick"
25
+ spec.add_development_dependency "activesupport"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "rspec-rails"
28
+ spec.add_development_dependency "pry"
29
+ spec.add_development_dependency "pry-doc"
30
+ spec.add_development_dependency "travis-lint"
31
+ spec.add_development_dependency "codeclimate-test-reporter"
32
+ spec.add_development_dependency "rspec-html-matchers"
33
+ end
@@ -0,0 +1,6 @@
1
+ require 'chartkick/remote/helper'
2
+ require 'chartkick/remote/remote'
3
+
4
+ ActiveSupport.on_load(:action_view) do
5
+ include Chartkick::Remote::Helper
6
+ end
@@ -0,0 +1,59 @@
1
+ require 'active_support/core_ext'
2
+ require 'chartkick'
3
+
4
+ module Chartkick::Remote
5
+ module Helper
6
+ include Chartkick::Helper
7
+
8
+ %w{line_chart pie_chart column_chart bar_chart area_chart geo_chart}.each do |type|
9
+ define_method :"#{type}_with_remote" do |data_source = nil, options = {}, &block|
10
+ chartkick_remote_chart type, data_source, options, &block
11
+ end
12
+
13
+ alias_method_chain :"#{type}", :remote
14
+ end
15
+
16
+ private
17
+
18
+ def chartkick_remote_chart(type, data_source, options, &block)
19
+ if block_given? && data_source.is_a?(Hash)
20
+ options = data_source
21
+ end
22
+
23
+ options = options.dup
24
+ options.reverse_merge!(controller.chartkick_options) if controller.respond_to?(:chartkick_options)
25
+
26
+ standalone = options.delete(:standalone)
27
+ remote = options.delete(:remote)
28
+ skip = false
29
+
30
+ if remote
31
+ @remote_chart_id = (@remote_chart_id || 0) + 1
32
+ chart_id = controller.params[:_chartkick_remote_chart_id]
33
+ if chart_id # json request
34
+ controller.chartkick_remote_blocks ||= {}
35
+ controller.chartkick_remote_blocks[@remote_chart_id] = block
36
+ skip = standalone && chart_id.to_s != @remote_chart_id.to_s
37
+ else
38
+ data_source = url_for(params.merge(_chartkick_remote_chart_id: @remote_chart_id, format: :json))
39
+ end
40
+ elsif block_given?
41
+ data_source = block.call
42
+ end
43
+
44
+ result = send(:"#{type}_without_remote", data_source, options)
45
+
46
+ if remote && standalone
47
+ result = '<div>Skipped</div>'.html_safe if skip
48
+
49
+ standalone_link = link_to 'Standalone',
50
+ url_for(params.merge(_chartkick_remote_chart_id: @remote_chart_id,
51
+ _chartkick_remote_standalone: 1))
52
+
53
+ result += standalone_link.html_safe
54
+ end
55
+
56
+ result
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,46 @@
1
+ module Chartkick::Remote
2
+ extend ActiveSupport::Concern
3
+ attr_accessor :chartkick_remote_blocks
4
+ attr_accessor :chartkick_options
5
+
6
+ included do
7
+ class_attribute :chartkick_options
8
+ end
9
+
10
+ module Responder
11
+ def to_json
12
+ controller.render_to_string(options.merge(formats: [:html], layout: false))
13
+ data_source = controller.chartkick_remote_blocks[controller.params[:_chartkick_remote_chart_id].to_i].call
14
+ data_source = data_source.chart_json if data_source.respond_to?(:chart_json)
15
+ render json: data_source
16
+ end
17
+ end
18
+
19
+ def default_render(*)
20
+ if params[:_chartkick_remote_chart_id] && !params[:_chartkick_remote_standalone]
21
+ respond_with nil
22
+ else
23
+ super
24
+ end
25
+ end
26
+
27
+ module ClassMethods
28
+ def chartkick_remote(options = {})
29
+ options = options.dup
30
+
31
+ action_filter_options = options.extract!(:only, :except)
32
+
33
+ respond_to :json, action_filter_options
34
+
35
+ self.responder = Class.new(responder) do
36
+ include Responder
37
+ end
38
+
39
+ before_filter action_filter_options do
40
+ self.chartkick_options = self.class.chartkick_options
41
+ end
42
+
43
+ self.chartkick_options = {remote: true}.merge(options)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,5 @@
1
+ module Chartkick
2
+ module Remote
3
+ VERSION = "1.0.2"
4
+ end
5
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+ require 'chartkick/remote'
3
+
4
+ AnonymousRoutes = ActionDispatch::Routing::RouteSet.new.tap do |routes|
5
+ routes.draw { resources :anonymous }
6
+ end
7
+
8
+ describe Chartkick::Remote, type: :controller do
9
+ render_views
10
+
11
+ AnonymousController = Class.new(ActionController::Base) do
12
+ include Chartkick::Remote
13
+
14
+ prepend_view_path 'spec/controllers/views'
15
+
16
+ include AnonymousRoutes.url_helpers
17
+ helper AnonymousRoutes.url_helpers
18
+
19
+ def index
20
+ end
21
+ end
22
+
23
+ controller AnonymousController do
24
+ chartkick_remote
25
+ end
26
+
27
+ describe "GET" do
28
+ routes { AnonymousRoutes }
29
+
30
+ it "generates a remote data source" do
31
+ get :index, format: :html
32
+ expect(response.body).to include '_chartkick_remote_chart_id=1'
33
+ end
34
+
35
+ it "returns the remote data source as json" do
36
+ get :index, _chartkick_remote_chart_id: 1, format: :json
37
+ expect(JSON.parse(response.body)).to eq [[0,1]]
38
+ end
39
+
40
+ describe "when the standalone option is set" do
41
+ controller AnonymousController do
42
+ chartkick_remote standalone: true
43
+ end
44
+
45
+ it "does not show any other charts but the selected chart" do
46
+ get :index, _chartkick_remote_chart_id: 1, _chartkick_remote_standalone: 1, format: :html
47
+
48
+ expect(response.body).to have_tag :div, 'Skipped' #, count: 1
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,2 @@
1
+ <%= line_chart { [[0,1]] } %>
2
+ <%= line_chart { [[1,2]] } %>
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+ require 'chartkick/remote'
3
+
4
+ AnonymousRoutes = ActionDispatch::Routing::RouteSet.new.tap do |routes|
5
+ routes.draw { resources :anonymous }
6
+ end
7
+
8
+ describe Chartkick::Remote::Helper, type: :helper do
9
+ describe "when the standalone option is set" do
10
+ it "includes a link to the standalone version of the chart" do
11
+
12
+ @controller.singleton_class.class_eval do
13
+ include Chartkick::Remote
14
+ include AnonymousRoutes.url_helpers
15
+ chartkick_remote
16
+ end
17
+
18
+ helper.singleton_class.class_eval do
19
+ include AnonymousRoutes.url_helpers
20
+ end
21
+
22
+ @controller.params.merge!(
23
+ action: :index,
24
+ controller: :anonymous
25
+ )
26
+
27
+ expect(helper.line_chart(standalone: true) { [[0,1]] }).to have_tag :a #, with: { href: /_chartkick_remote_standalone/ }
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,16 @@
1
+ require 'codeclimate-test-reporter'
2
+ CodeClimate::TestReporter.start
3
+
4
+ require 'active_support'
5
+ require 'action_view'
6
+ require 'action_controller'
7
+ require 'rails/engine'
8
+ require 'rspec/rails'
9
+ require 'rspec-html-matchers'
10
+ require 'pry'
11
+
12
+ module Rails
13
+ def self.application
14
+ OpenStruct.new(routes: nil, env_config: {})
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,232 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chartkick-remote
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Andrew S. Brown
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-30 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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: minitest
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: chartkick
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: activesupport
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: rspec
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: pry
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: pry-doc
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
+ - !ruby/object:Gem::Dependency
140
+ name: travis-lint
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: codeclimate-test-reporter
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: rspec-html-matchers
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ description: Automatically generate remote json for chartkick
182
+ email:
183
+ - andrew@dontfidget.com
184
+ executables: []
185
+ extensions: []
186
+ extra_rdoc_files: []
187
+ files:
188
+ - ".gitignore"
189
+ - ".travis.yml"
190
+ - Gemfile
191
+ - LICENSE.txt
192
+ - README.md
193
+ - Rakefile
194
+ - chartkick_remote.gemspec
195
+ - lib/chartkick/remote.rb
196
+ - lib/chartkick/remote/helper.rb
197
+ - lib/chartkick/remote/remote.rb
198
+ - lib/chartkick/remote/version.rb
199
+ - spec/controllers/remote_spec.rb
200
+ - spec/controllers/views/anonymous/index.html.erb
201
+ - spec/helpers/helper_spec.rb
202
+ - spec/spec_helper.rb
203
+ homepage: ''
204
+ licenses:
205
+ - MIT
206
+ metadata: {}
207
+ post_install_message:
208
+ rdoc_options: []
209
+ require_paths:
210
+ - lib
211
+ required_ruby_version: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - ">="
214
+ - !ruby/object:Gem::Version
215
+ version: '0'
216
+ required_rubygems_version: !ruby/object:Gem::Requirement
217
+ requirements:
218
+ - - ">="
219
+ - !ruby/object:Gem::Version
220
+ version: '0'
221
+ requirements: []
222
+ rubyforge_project:
223
+ rubygems_version: 2.2.2
224
+ signing_key:
225
+ specification_version: 4
226
+ summary: Automatically generate remote json for chartkick
227
+ test_files:
228
+ - spec/controllers/remote_spec.rb
229
+ - spec/controllers/views/anonymous/index.html.erb
230
+ - spec/helpers/helper_spec.rb
231
+ - spec/spec_helper.rb
232
+ has_rdoc: