compositor-rails 0.0.1

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
+ SHA1:
3
+ metadata.gz: 14194e2d62f07dd1a73e94389bcd813b03b6e1f9
4
+ data.tar.gz: e070253a8a7681d41add07f7871baa3061ef5fa2
5
+ SHA512:
6
+ metadata.gz: 3318b786d79ffe6c98e9368aac81d9a5341e4d4d0358480d01825a482dafb3aa5972acd24c9625d54b62f7705cfaea4ce0775df742eafe0abfb80e75e666838d
7
+ data.tar.gz: 9e935e9bfc5b3b418d44d69cd510e4fbfa737a5b73fe40e870a2e77f2aab46d2aac824d5b817990791ff07ddabfdb8876758c545e6e98495cfc06adf9bd20fc7
@@ -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
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in compositor-rails.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 TODO: Write your name
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,87 @@
1
+ # Compositor::Rails
2
+
3
+ Extensions to compositor that allow views to be created and used inside of Rails.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'compositor-rails'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install compositor-rails
18
+
19
+ ## Usage
20
+
21
+ Given a `UserCompositor` as follows:
22
+
23
+ ```ruby
24
+ # File: app/compositors/user_compositor.rb
25
+ class UserCompositor < Compositor::Leaf
26
+ attr_accessor :user
27
+
28
+ def initialize(context, user, attrs = {})
29
+ super(context, attrs)
30
+ self.user = user
31
+ end
32
+
33
+ def to_hash
34
+ {
35
+ id: user.id,
36
+ username: user.username,
37
+ location: user.location,
38
+ bio: user.bio,
39
+ url: user.url,
40
+ image_url: context.image_path(user.avatar), # using context to generate URL path from routes
41
+ ...
42
+ }
43
+ end
44
+ end
45
+ ```
46
+
47
+ To render a list of users, create a view and then write out the DSL as you normally would.
48
+
49
+ ```ruby
50
+ # File: app/views/users/index.json.compositor
51
+ list :collection: @users, root: :users do |u|
52
+ user: u
53
+ end
54
+ ```
55
+
56
+ ### Partials
57
+
58
+ Let's move the rendering of a user into a partial.
59
+
60
+ ```ruby
61
+ # File: app/views/users/index.json.compositor
62
+ list :collection: @users, root: :users do |u|
63
+ partial! 'show', user: user
64
+ end
65
+ ```
66
+
67
+ ```ruby
68
+ # File: app/views/users/show.json.compositor
69
+ user: user
70
+ ```
71
+
72
+ Notes on using partials:
73
+
74
+ 1. Try and keep them to a minimum. The whole point of compositor is to reduce complexity
75
+ in the composition stage. A enourmous number of partials might just mean a need for
76
+ another compositor.
77
+ 2. Normal Rails partials support collections. These do not as compositor requires you to
78
+ explicitly define your collection rendering. Of course a partial can render a collection
79
+ itself, by using the normal compositor list/map functionality.
80
+
81
+ ## Contributing
82
+
83
+ 1. Fork it ( http://github.com/<my-github-username>/compositor-rails/fork )
84
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
85
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
86
+ 4. Push to the branch (`git push origin my-new-feature`)
87
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -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 'compositor/rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "compositor-rails"
8
+ spec.version = Compositor::Rails::VERSION
9
+ spec.authors = ["Paul Henry"]
10
+ spec.email = ["paul@wanelo.com"]
11
+ spec.summary = %q{ActionView extensions to compositor.}
12
+ spec.description = %q{VIEWS WITH COMPOSITOR YAY}
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 'compositor'
22
+ spec.add_dependency 'activesupport', '>= 3.0.0'
23
+
24
+ spec.add_development_dependency "actionpack"
25
+ spec.add_development_dependency "bundler", "~> 1.5"
26
+ spec.add_development_dependency "pry"
27
+ spec.add_development_dependency "rake"
28
+ spec.add_development_dependency "rspec"
29
+ spec.add_development_dependency "rspec-rails"
30
+ spec.add_development_dependency "railties"
31
+ end
@@ -0,0 +1,10 @@
1
+ require "compositor/rails/version"
2
+
3
+ module Compositor
4
+ module Rails
5
+ # Your code goes here...
6
+ end
7
+ end
8
+
9
+ require "compositor/rails/dsl"
10
+ require "compositor/rails/handler"
@@ -0,0 +1,63 @@
1
+ require 'compositor'
2
+
3
+ module Compositor
4
+ module Rails
5
+ class DSL < Compositor::DSL
6
+
7
+ def initialize(*args)
8
+ super
9
+ @partial_variable_counter = 0
10
+ end
11
+
12
+ def partial!(name_or_options, locals = {})
13
+ case name_or_options
14
+ when ::Hash
15
+ # partial! partial: 'name', locals: { foo: 'bar' }
16
+ options = name_or_options
17
+ else
18
+ # partial! 'name', foo: 'bar'
19
+ options = { partial: name_or_options, locals: locals }
20
+ as = locals.delete(:as)
21
+ options[:as] = as if as.present?
22
+ options[:collection] = locals[:collection] if locals.key?(:collection)
23
+ end
24
+
25
+ _handle_partial_options options
26
+ end
27
+
28
+ private
29
+
30
+ def _handle_partial_options(options)
31
+ options.reverse_merge! locals: {}
32
+ options.reverse_merge! ::Compositor::Rails::Handler.default_partial_options
33
+ _render_partial options
34
+ end
35
+
36
+ def _render_partial(options)
37
+ template = _lookup_context.find_template(options[:partial],
38
+ [],
39
+ true,
40
+ [],
41
+ options)
42
+
43
+ instance_eval _compile_partial(template, options)
44
+ end
45
+
46
+ def _compile_partial(template, options)
47
+ source = ""
48
+ options[:locals].each do |k, v|
49
+ name = "@partial_#{@partial_variable_counter}"
50
+ instance_variable_set name, v
51
+ source << "#{k} = #{name};"
52
+ @partial_variable_counter += 1
53
+ end
54
+ source << template.source
55
+ source
56
+ end
57
+
58
+ def _lookup_context
59
+ context.controller.view_renderer.lookup_context
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,20 @@
1
+ module Compositor
2
+ module Rails
3
+ class Handler
4
+ cattr_accessor :default_format
5
+ self.default_format = Mime::JSON
6
+
7
+ def self.default_partial_options
8
+ { format: self.default_format }
9
+ end
10
+
11
+ def self.call(template)
12
+ # this juggling is required to keep line numbers right in the error
13
+ %{Compositor::Rails::DSL.create(self) do #{template.source}
14
+ end.to_json}
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ ActionView::Template.register_template_handler :compositor, Compositor::Rails::Handler
@@ -0,0 +1,5 @@
1
+ module Compositor
2
+ module Rails
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ describe Compositor::Rails::DSL do
4
+ end
@@ -0,0 +1,29 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ require 'action_view'
9
+ require 'action_controller'
10
+ require 'action_view/testing/resolvers'
11
+ require 'active_support/cache'
12
+ require 'json'
13
+ require 'rails/railtie'
14
+ require 'rspec/rails'
15
+ require 'compositor/rails'
16
+
17
+ require 'support/compositors'
18
+
19
+ RSpec.configure do |config|
20
+ config.treat_symbols_as_metadata_keys_with_true_values = true
21
+ config.run_all_when_everything_filtered = true
22
+ config.filter_run :focus
23
+
24
+ # Run specs in random order to surface order dependencies. If you find an
25
+ # order dependency and want to debug it, you can fix the order by providing
26
+ # the seed, which is printed after each run.
27
+ # --seed 1234
28
+ config.order = 'random'
29
+ end
@@ -0,0 +1,12 @@
1
+ require 'compositor'
2
+ require 'oj'
3
+
4
+ class StringCompositor < Compositor::Leaf
5
+ attr_accessor :string
6
+
7
+ def to_hash
8
+ with_root_element do
9
+ { 'string' => string.to_s }
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Compositor views', type: :view do
4
+
5
+ let(:source) do
6
+ <<-RUBY
7
+ string string: 'hello'
8
+ RUBY
9
+ end
10
+
11
+ let(:partials) {
12
+ {'test.json.compositor' => source}
13
+ }
14
+
15
+ let(:result) { JSON.parse(ActionView::Template.new(source, 'test', Compositor::Rails::Handler, :virtual_path => 'test').render(self, {}).strip) }
16
+
17
+ before do
18
+ lookup_context.view_paths = [ActionView::FixtureResolver.new(partials)]
19
+ end
20
+
21
+ it 'renders the compositor' do
22
+ expect(result['string']).to eq('hello')
23
+ end
24
+
25
+
26
+ describe '#partial' do
27
+ let(:source) do
28
+ <<-RUBY
29
+ partial! 'boom', explosion: 'hai'
30
+ RUBY
31
+ end
32
+
33
+ let(:partial) do
34
+ <<-RUBY
35
+ string string: explosion
36
+ RUBY
37
+ end
38
+
39
+ let(:partials) { { 'test.json.compositor' => source, '_boom.json.compositor' => partial } }
40
+
41
+ it 'renders the partial' do
42
+ expect(result['string']).to eq('hai')
43
+ end
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,190 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: compositor-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Paul Henry
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: compositor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 3.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: actionpack
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: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
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: rake
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
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: rspec-rails
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: railties
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: VIEWS WITH COMPOSITOR YAY
140
+ email:
141
+ - paul@wanelo.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - Gemfile
149
+ - LICENSE.txt
150
+ - README.md
151
+ - Rakefile
152
+ - compositor-rails.gemspec
153
+ - lib/compositor/rails.rb
154
+ - lib/compositor/rails/dsl.rb
155
+ - lib/compositor/rails/handler.rb
156
+ - lib/compositor/rails/version.rb
157
+ - spec/lib/compositor/rails/dsl_spec.rb
158
+ - spec/spec_helper.rb
159
+ - spec/support/compositors.rb
160
+ - spec/views/view_spec.rb
161
+ homepage: ''
162
+ licenses:
163
+ - MIT
164
+ metadata: {}
165
+ post_install_message:
166
+ rdoc_options: []
167
+ require_paths:
168
+ - lib
169
+ required_ruby_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ requirements: []
180
+ rubyforge_project:
181
+ rubygems_version: 2.2.0
182
+ signing_key:
183
+ specification_version: 4
184
+ summary: ActionView extensions to compositor.
185
+ test_files:
186
+ - spec/lib/compositor/rails/dsl_spec.rb
187
+ - spec/spec_helper.rb
188
+ - spec/support/compositors.rb
189
+ - spec/views/view_spec.rb
190
+ has_rdoc: