rjax 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: 05b33d7cc946a1aaa2f9a5277644700d402ceade
4
+ data.tar.gz: ed4585c29a78e0aadff71aa550de05eee220e1dd
5
+ SHA512:
6
+ metadata.gz: adc9d0fcaa58363020f6c320c2f2b96ca3ea7116558057373495c10365000d41152e95955b7a605ae6c27d658c4f93b986cae33f5418b5ca00a399bcbd4996a1
7
+ data.tar.gz: d2ba335efade55bf37327857c55d3b194353adc1d350789de294cd732e65cf0d11c5503e33aa5cf25ec7fc4fe3b65c82ce78ed250ec6547649d2ac9f748d15a4
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ tmp
16
+ /gemfiles/*.lock
17
+ /.rspec
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - rbx-19mode
6
+ - jruby-19mode
7
+ gemfile:
8
+ - gemfiles/rails_31
9
+ - gemfiles/rails_32
10
+ - gemfiles/rails_40
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rails'
6
+ gem 'debugger', :platform => [:mri_19, :mri_20]
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Sergey Pchelincev
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.
data/README.rdoc ADDED
@@ -0,0 +1,79 @@
1
+ = Rjax
2
+
3
+ Solve template dispatch on ajax request
4
+
5
+ {<img src="https://travis-ci.org/jalkoby/rjax.png?branch=master" alt="Build Status" />}[https://travis-ci.org/jalkoby/rjax]
6
+ {<img src="https://travis-ci.org/jalkoby/rjax.png?branch=master" alt="Build Status" />}[https://travis-ci.org/jalkoby/rjax]
7
+ {<img src="https://badge.fury.io/rb/rjax.png" alt="Gem Version" />}[http://badge.fury.io/rb/rjax]
8
+
9
+ == Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'rjax'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rjax
22
+
23
+ == Usage
24
+
25
+ Rjax will be useful if you have a lot action which serve http and ajax request. It might looks like:
26
+
27
+ def index
28
+ @users = User.all
29
+ render :partials => 'users', :locals => { :users => @users } if request.xhr?
30
+ # or
31
+ render :index_ajax if request.xhr?
32
+ end
33
+
34
+ Rjax replace it with single method call
35
+
36
+ def index
37
+ @users = User.all
38
+ rjax
39
+ end
40
+ # render index_rjax template
41
+
42
+ def index
43
+ @users = User.all
44
+ rjax @users
45
+ end
46
+ # render _user_rjax or _user partial with variable `user`
47
+
48
+ def index
49
+ @users = User.all
50
+ rjax :users => @users, :other => :variable
51
+ end
52
+ # render _users_rjax or _users partial with variables users & other
53
+
54
+ def show
55
+ @user = User.find(params[:id])
56
+ rjax @user
57
+ end
58
+ # render _user_rjax or _user partial with `user` variable
59
+
60
+ The name of partial is based on controller name. If you don't like rjax suffix or it's out your team's convention
61
+ feel free to configure wrapper:
62
+
63
+ # config/initializers/rjax.rb
64
+ Rjax.config do |config|
65
+ config.suffix = "suffix"
66
+ config.prefix = "prefix"
67
+ end
68
+
69
+ prefix_%action_name%_suffix
70
+
71
+ _prefix_%partial_name%_suffix
72
+
73
+ == Contributing
74
+
75
+ 1. Fork it
76
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
77
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
78
+ 4. Push to the branch (`git push origin my-new-feature`)
79
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ desc "Run all specs"
7
+ task :default => :spec
data/gemfiles/rails_31 ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails', '~> 3.1.0'
4
+
5
+ gemspec :path => ".."
data/gemfiles/rails_32 ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails', '~> 3.2.0'
4
+
5
+ gemspec :path => ".."
data/gemfiles/rails_40 ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails', '~> 4.0.0'
4
+
5
+ gemspec :path => ".."
data/lib/rjax.rb ADDED
@@ -0,0 +1,28 @@
1
+ require 'active_support'
2
+
3
+ module Rjax
4
+ autoload :Config, 'rjax/config'
5
+ autoload :InvalidConfigurationError, 'rjax/errors'
6
+ autoload :Resolver, 'rjax/resolver'
7
+ autoload :PartialMissingError, 'rjax/errors'
8
+
9
+ def self.config(&block)
10
+ @config ||= ::Rjax::Config.new
11
+ if block_given?
12
+ yield(@config)
13
+ @config.validate!
14
+ else
15
+ @config
16
+ end
17
+ end
18
+ end
19
+
20
+ ActiveSupport.on_load(:action_controller) do
21
+ class_eval do
22
+ private
23
+
24
+ def rjax(options = nil)
25
+ render(Rjax::Resolver.process(options, self)) if request.xhr?
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,20 @@
1
+ class Rjax::Config
2
+ attr_accessor :prefix, :suffix
3
+
4
+ def suffix
5
+ return @suffix if defined?(@suffix)
6
+ @suffix = 'rjax'
7
+ end
8
+
9
+ def suffix?
10
+ suffix && !suffix.empty?
11
+ end
12
+
13
+ def prefix?
14
+ prefix && !prefix.empty?
15
+ end
16
+
17
+ def validate!
18
+ raise Rjax::InvalidConfigurationError, "provide suffix or prefix" unless suffix? || prefix?
19
+ end
20
+ end
@@ -0,0 +1,11 @@
1
+ module Rjax
2
+ class BaseError < ::StandardError
3
+ end
4
+
5
+ class InvalidConfigurationError < BaseError
6
+ end
7
+
8
+ class PartialMissingError < BaseError
9
+ end
10
+ end
11
+
@@ -0,0 +1,57 @@
1
+ require 'active_support/core_ext/string'
2
+
3
+ class Rjax::Resolver < Struct.new(:options, :controller)
4
+ def self.process(*args)
5
+ new(*args).process
6
+ end
7
+
8
+ def process
9
+ case options
10
+ when Hash then general_partial
11
+ when Array then collection_partial
12
+ when NilClass then template_name
13
+ else
14
+ instance_partial
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def general_partial
21
+ { :partial => partial_name(:multi), :locals => options, :layout => false }
22
+ end
23
+
24
+ def collection_partial
25
+ { :partial => partial_name, :collection => options, :as => singular_name, :layout => false }
26
+ end
27
+
28
+ def instance_partial
29
+ { :partial => partial_name, :locals => { singular_name.to_sym => options }, :layout => false }
30
+ end
31
+
32
+ def template_name
33
+ rjax_version(controller.action_name).to_sym
34
+ end
35
+
36
+ def partial_name(multi = false)
37
+ name = multi ? singular_name.pluralize : singular_name
38
+ result = [rjax_version(name), name].detect { |item| controller.template_exists?(item, prefixes, true) }
39
+ result ? result : raise(Rjax::PartialMissingError, name)
40
+ end
41
+
42
+ def singular_name
43
+ @singular_name ||= controller.controller_name.singularize
44
+ end
45
+
46
+ def rjax_version(base)
47
+ result = ""
48
+ result << "#{ Rjax.config.prefix }_" if Rjax.config.prefix?
49
+ result << base
50
+ result << "_#{ Rjax.config.suffix }" if Rjax.config.suffix?
51
+ result
52
+ end
53
+
54
+ def prefixes
55
+ @prefixes ||= controller.send(:_normalize_render)[:prefixes]
56
+ end
57
+ end
@@ -0,0 +1,3 @@
1
+ module Rjax
2
+ VERSION = "0.1.0"
3
+ end
data/rjax.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rjax/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rjax"
8
+ spec.version = Rjax::VERSION
9
+ spec.authors = ["Sergey Pchelincev"]
10
+ spec.email = ["jalkoby91@gmail.com"]
11
+ spec.description = %q{Solve template dispatch on ajax request}
12
+ spec.summary = spec.description
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_dependency "actionpack", ">= 3.1.0"
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec", "~> 2.14.0"
25
+ spec.add_development_dependency "rack-test"
26
+ end
@@ -0,0 +1,63 @@
1
+ require 'action_controller/railtie'
2
+ require 'action_view/railtie'
3
+
4
+ class Dummy < Rails::Application
5
+ # Set Rails.root to the same folder as this file
6
+ config.root = File.dirname(__FILE__)
7
+
8
+ # Rails needs these keys, but they don't really have to be secret for our tests
9
+ config.session_store :cookie_store, key: '****************************************'
10
+ config.secret_token = '****************************************'
11
+
12
+ config.logger = Logger.new(STDOUT)
13
+ Rails.logger = config.logger
14
+
15
+ config.action_controller.view_path
16
+
17
+ # Our routes
18
+ routes.draw do
19
+ resources :users, :only => [:index, :show] do
20
+ get :search, :popular, :on => :collection
21
+ end
22
+ resources :articles, :only => [:index, :show] do
23
+ get :search, :on => :collection
24
+ end
25
+ end
26
+ end
27
+
28
+ class BaseController < ActionController::Base
29
+ prepend_view_path Rails.root
30
+ end
31
+
32
+ class UsersController < BaseController
33
+ def index
34
+ @users = %w(Sam Alise John)
35
+ rjax
36
+ end
37
+
38
+ def search
39
+ rjax :users => %w(Sam John)
40
+ end
41
+
42
+ def popular
43
+ rjax %w(Jessy Walt)
44
+ end
45
+
46
+ def show
47
+ rjax "Alex"
48
+ end
49
+ end
50
+
51
+ class ArticlesController < BaseController
52
+ def index
53
+ rjax %w(Monday Tuesday Sunday)
54
+ end
55
+
56
+ def search
57
+ rjax :articles => %w(Monday Sunday)
58
+ end
59
+
60
+ def show
61
+ rjax "Monday"
62
+ end
63
+ end
@@ -0,0 +1 @@
1
+ <%= article %>
@@ -0,0 +1 @@
1
+ <%= articles.join(', ') %>
@@ -0,0 +1 @@
1
+ layout - <%= yield %>
@@ -0,0 +1 @@
1
+ <%= user %>
@@ -0,0 +1 @@
1
+ <%= users.join(', ') %>
@@ -0,0 +1 @@
1
+ <%= @users.join(', ') %>
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rjax::Resolver do
4
+ context '#rjax_version' do
5
+ let(:controller) { double("Controller", :action_name => "index" ) }
6
+ let(:config) { Rjax.config }
7
+ subject { described_class.process(nil, controller) }
8
+
9
+ specify { should == :index_rjax }
10
+
11
+ it 'with custom suffix' do
12
+ config.suffix = "ajax"
13
+ should == :index_ajax
14
+ end
15
+
16
+ it 'with prefix' do
17
+ config.prefix = "super"
18
+ should == :super_index_rjax
19
+ end
20
+
21
+ it 'with prefix and without suffix' do
22
+ config.suffix = false
23
+ config.prefix = "ajax"
24
+ should == :ajax_index
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rjax, :type => :request do
4
+ let(:app) { Rails.application }
5
+ let(:body) { last_response.body.strip }
6
+
7
+ it 'rjax template' do
8
+ ajax '/users'
9
+
10
+ body.should == 'Sam, Alise, John'
11
+ end
12
+
13
+ it 'rjax general partial' do
14
+ ajax '/users/search'
15
+
16
+ body.should == 'Sam, John'
17
+ end
18
+
19
+ it 'rjax collection partial' do
20
+ ajax '/users/popular'
21
+
22
+ body.should == "Jessy\nWalt"
23
+ end
24
+
25
+ it 'rjax instance template' do
26
+ ajax '/users/1'
27
+
28
+ body.should == 'Alex'
29
+ end
30
+
31
+ it 'convention general partial' do
32
+ ajax '/articles'
33
+
34
+ body.should == "Monday\nTuesday\nSunday"
35
+ end
36
+
37
+ it 'convention collection partial' do
38
+ ajax '/articles/search'
39
+
40
+ body.should == "Monday, Sunday"
41
+ end
42
+
43
+ it 'collection instance partial' do
44
+ ajax '/articles/1'
45
+
46
+ body.should == "Monday"
47
+ end
48
+
49
+ it 'raise error is configurated incorrect' do
50
+ expect do
51
+ described_class.config do |config|
52
+ config.suffix = ""
53
+ config.prefix = ""
54
+ end
55
+ end.to raise_error(Rjax::InvalidConfigurationError)
56
+ end
57
+
58
+ def ajax(path)
59
+ get path, {}, { "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest", 'HTTP_ACCEPT' => '*/*' }
60
+ end
61
+ end
@@ -0,0 +1,19 @@
1
+ require 'bundler/setup'
2
+ Bundler.require(:default)
3
+
4
+ ENV["RAILS_ENV"] ||= 'test'
5
+ require "#{ File.dirname(__FILE__) }/dummy/application"
6
+
7
+ Dir["#{ File.dirname(__FILE__) }/support/**/*.rb"].each { |f| require f }
8
+
9
+ RSpec.configure do |config|
10
+ config.treat_symbols_as_metadata_keys_with_true_values = true
11
+ config.filter_run :focus => true
12
+ config.run_all_when_everything_filtered = true
13
+
14
+ config.include Rack::Test::Methods, :type => :request
15
+
16
+ config.before(:each) do
17
+ Rjax.instance_variable_set("@config", nil)
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rjax
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Pchelincev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionpack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
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: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.14.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 2.14.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack-test
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
+ description: Solve template dispatch on ajax request
84
+ email:
85
+ - jalkoby91@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .travis.yml
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.rdoc
95
+ - Rakefile
96
+ - gemfiles/rails_31
97
+ - gemfiles/rails_32
98
+ - gemfiles/rails_40
99
+ - lib/rjax.rb
100
+ - lib/rjax/config.rb
101
+ - lib/rjax/errors.rb
102
+ - lib/rjax/resolver.rb
103
+ - lib/rjax/version.rb
104
+ - rjax.gemspec
105
+ - spec/dummy/application.rb
106
+ - spec/dummy/articles/_article.erb
107
+ - spec/dummy/articles/_articles.erb
108
+ - spec/dummy/layouts/rjax.erb
109
+ - spec/dummy/users/_user_rjax.erb
110
+ - spec/dummy/users/_users_rjax.erb
111
+ - spec/dummy/users/index_rjax.erb
112
+ - spec/rjax/rjax_resolver_spec.rb
113
+ - spec/rjax/rjax_spec.rb
114
+ - spec/spec_helper.rb
115
+ homepage: ''
116
+ licenses:
117
+ - MIT
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.0.3
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: Solve template dispatch on ajax request
139
+ test_files:
140
+ - spec/dummy/application.rb
141
+ - spec/dummy/articles/_article.erb
142
+ - spec/dummy/articles/_articles.erb
143
+ - spec/dummy/layouts/rjax.erb
144
+ - spec/dummy/users/_user_rjax.erb
145
+ - spec/dummy/users/_users_rjax.erb
146
+ - spec/dummy/users/index_rjax.erb
147
+ - spec/rjax/rjax_resolver_spec.rb
148
+ - spec/rjax/rjax_spec.rb
149
+ - spec/spec_helper.rb