netzke-draper 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ Gemfile.lock
3
+ .bundle
4
+ pkg/
5
+ *sublime*
6
+ .yardoc/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem 'netzke-core', :github => 'nomadcoder/netzke-core'
6
+ gem 'netzke-basepack', :github => 'nomadcoder/netzke-basepack'
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Georg Meyer
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,95 @@
1
+ # netzke-draper [<img src="https://secure.travis-ci.org/scho/netzke-draper.png" />](http://travis-ci.org/scho/netzke-draper)
2
+
3
+ netzke-draper is a simple extension for all [netzke](http://netzke.org/) components, that use the [data accessor](https://github.com/nomadcoder/netzke-basepack/blob/master/lib/netzke/basepack/data_accessor.rb) module (e.g. GridPanel). It automatically decorates your record(s) with a given decorator or one that matches the default naming convention.
4
+
5
+ ## Installation
6
+
7
+ In Rails 3 add this to your gem file and run the ```bundle``` command.
8
+
9
+ ```
10
+ gem 'netzke-draper'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Simply set the class of your decorator of choice in the netzke configure method.
16
+
17
+ ## Example
18
+
19
+ Given the following grid panel:
20
+
21
+ ```ruby
22
+ class BooksGridPanel < Netzke::Basepack::GridPanel
23
+
24
+ def configure(c)
25
+ c.model = 'Book'
26
+ c.decorator = 'BookDecorator'
27
+ end
28
+
29
+ end
30
+ ```
31
+
32
+ and a decorator class for your book model:
33
+
34
+ ```ruby
35
+ class BookDecorator < Draper::Base
36
+
37
+ decorates :book
38
+
39
+ def funky_title
40
+ "#{title} - BOOM!"
41
+ end
42
+
43
+ end
44
+ ```
45
+
46
+ Now you can use all decorator methods (in this case ```funky_title```) in your column definitions of the grid panel, since it automatically wraps all records with your decorator:
47
+
48
+ ```ruby
49
+ class BooksGridPanel < Netzke::Basepack::GridPanel
50
+
51
+ # [...]
52
+
53
+ column :funky_title do |c|
54
+ c.text = "Funky Title"
55
+ end
56
+ end
57
+ ```
58
+
59
+ ### Which decorator will be used?
60
+
61
+ 1. If you define a decorator in your config, that one will be used:
62
+
63
+ ```ruby
64
+ def configure(c)
65
+ c.decorator = 'YourDecorator'
66
+ end
67
+ ```
68
+
69
+ 2. If no decorator is defined, it will try to gues your decorator's class by appending 'Decorator' to your model class.
70
+
71
+ 3. If you set ```c.decorator``` to false, it won't decorate anything, even if a guess would be successful.
72
+
73
+ 4. You can also disable the default usage of a decorator with an initializer. In this case, only if there is an explicit definition of a decorator in the configuration, it will decorate your records:
74
+
75
+ ```ruby
76
+ # config/initializers/netzke-draper.rb
77
+ Netzke::Draper.configure do |config|
78
+ config.decorate_by_default = false
79
+ end
80
+ ```
81
+
82
+ ## Contributing to netzke-draper
83
+
84
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
85
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
86
+ * Fork the project.
87
+ * Start a feature/bugfix branch.
88
+ * Commit and push until you are happy with your contribution.
89
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
90
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
91
+
92
+ ## Copyright
93
+
94
+ Copyright (c) 2012 Georg Meyer. See LICENSE.txt for
95
+ further details.
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rspec/core/rake_task'
4
+ require 'bundler'
5
+
6
+ Bundler::GemHelper.install_tasks
7
+
8
+ desc "Run RSpec"
9
+ RSpec::Core::RakeTask.new do |t|
10
+ t.verbose = false
11
+ end
12
+
13
+ task :default => :spec
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'netzke/draper'
@@ -0,0 +1 @@
1
+ require 'netzke/draper'
@@ -0,0 +1,21 @@
1
+ require 'netzke/basepack/data_accessor'
2
+ require 'netzke/basepack/data_adapters/abstract_adapter'
3
+ require 'netzke/basepack/data_adapters/active_record_adapter'
4
+
5
+ require 'netzke/draper/configuration'
6
+ require 'netzke/draper/basepack_ext/data_accessor'
7
+ require 'netzke/draper/basepack_ext/data_adapters/abstract_adapter'
8
+ require 'netzke/draper/basepack_ext/data_adapters/active_record_adapter'
9
+
10
+ module Netzke
11
+ module Draper
12
+
13
+ class << self
14
+
15
+ def configure(&block)
16
+ Netzke::Draper::Configuration.configure(&block)
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,35 @@
1
+ module Netzke
2
+ module Basepack
3
+ module DataAccessor
4
+
5
+ def data_adapter
6
+ @data_adapter ||= data_class && Netzke::Basepack::DataAdapters::AbstractAdapter.adapter_class(data_class).new(data_class, decorator_class)
7
+ end
8
+
9
+ # Retrieves the decorator class depending on configuration
10
+ # and model class
11
+ #
12
+ # @return [Class, FalseClass]
13
+ def decorator_class
14
+ if @decorator_class.nil?
15
+ @decorator_class ||= if config[:decorator].is_a?(String)
16
+ config[:decorator].constantize
17
+ elsif config[:decorator].is_a?(Class)
18
+ config[:decorator]
19
+ elsif config.has_key?(:decorator)
20
+ false
21
+ elsif Netzke::Draper::Configuration.decorate_by_default
22
+ begin
23
+ "#{data_class}Decorator".constantize
24
+ rescue NameError
25
+ false
26
+ end
27
+ else
28
+ false
29
+ end
30
+ end
31
+ @decorator_class
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,40 @@
1
+ module Netzke::Basepack::DataAdapters
2
+ class AbstractAdapter
3
+
4
+ # Decorates the record retrieved by first
5
+ def first_with_decoration
6
+ decorate(first_without_decoration)
7
+ end
8
+ alias_method_chain :first, :decoration
9
+
10
+ # Decorates the record retrieved by find_record
11
+ def find_record_with_decoration(id)
12
+ decorate(find_record_without_decoration(id))
13
+ end
14
+ alias_method_chain :find_record, :decoration
15
+
16
+ # Decorates the record retrieved by new_record
17
+ def new_record_with_decoration(params = {})
18
+ decorate(new_record_without_decoration(params))
19
+ end
20
+ alias_method_chain :new_record, :decoration
21
+
22
+ # Override initializer to accept decorator_class
23
+ def initialize(model_class, decorator_class)
24
+ @model_class = model_class
25
+ @decorator_class = decorator_class
26
+ end
27
+
28
+ private
29
+
30
+ # Decorates record(s) if decorator_class is set
31
+ def decorate(record)
32
+ if @decorator_class
33
+ @decorator_class.decorate(record)
34
+ else
35
+ record
36
+ end
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,17 @@
1
+ module Netzke::Basepack::DataAdapters
2
+ class ActiveRecordAdapter
3
+
4
+ # Decorates the records retrieved by get_records
5
+ def get_records_with_decoration(params, columns=[])
6
+ decorate(get_records_without_decoration(params, columns))
7
+ end
8
+ alias_method_chain :get_records, :decoration
9
+
10
+ # Decorates the records retrieved by find_record
11
+ def find_record_with_decoration(id)
12
+ decorate(find_record_without_decoration(id))
13
+ end
14
+ alias_method_chain :find_record, :decoration
15
+
16
+ end
17
+ end
@@ -0,0 +1,28 @@
1
+ module Netzke
2
+ module Draper
3
+ module Configuration
4
+
5
+ def self.reset_config
6
+ configure do |config|
7
+ config.decorate_by_default = true
8
+ end
9
+ end
10
+
11
+ def self.configure
12
+ yield self
13
+ end
14
+
15
+ def self.decorate_by_default
16
+ @decorate_by_default
17
+ end
18
+
19
+ def self.decorate_by_default=(value)
20
+ @decorate_by_default = value
21
+ end
22
+
23
+ end
24
+ end
25
+ end
26
+
27
+ # Set default values
28
+ Netzke::Draper::Configuration.reset_config
@@ -0,0 +1,11 @@
1
+ module Netzke
2
+ module Draper
3
+ module Version
4
+ MAJOR = 0
5
+ MINOR = 0
6
+ PATCH = 1
7
+
8
+ STRING = [MAJOR, MINOR, PATCH].compact.join('.')
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,28 @@
1
+
2
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
3
+ require "netzke/draper/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "netzke-draper"
7
+ s.version = Netzke::Draper::Version::STRING
8
+ s.platform = Gem::Platform::RUBY
9
+ s.author = "Georg Meyer"
10
+ s.email = "georgmeyer83@googlemail.com"
11
+ s.homepage = "http://github.com/scho/netzke-draper"
12
+ s.summary = "Enables draper decorators in Netzke components"
13
+ s.description = "Gives you the ability to use any decorator in netzke components with almost no effort"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {spec}/*`.split("\n")
17
+ s.require_path = ["lib"]
18
+
19
+ s.add_development_dependency 'rspec', '~> 2.6.0'
20
+ s.add_development_dependency 'rake', '~> 0.9'
21
+ s.add_development_dependency 'rails', '~> 3.2.8'
22
+ s.add_development_dependency 'rspec-rails', '~> 2.6.1'
23
+
24
+ s.add_runtime_dependency 'draper'
25
+ s.add_runtime_dependency 'netzke-basepack'
26
+
27
+ s.required_rubygems_version = ">= 1.3.4"
28
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe Netzke::Basepack::DataAccessor do
4
+ describe "#decorator_class" do
5
+
6
+ before(:each) do
7
+ Netzke::Draper::Configuration.reset_config
8
+ end
9
+
10
+ it "by convention" do
11
+ c = DecoratedComponent.new
12
+ c.decorator_class.should == BookDecorator
13
+ end
14
+
15
+ it "by string" do
16
+ c = StringDecoratorComponent.new
17
+ c.decorator_class.should == NovelDecorator
18
+ end
19
+
20
+ it "by class" do
21
+ c = ClassDecoratorComponent.new
22
+ c.decorator_class.should == NovelDecorator
23
+ end
24
+
25
+ it "not set" do
26
+ c = UndecoratedComponent.new
27
+ c.decorator_class.should be_false
28
+ end
29
+
30
+ it "not set by config" do
31
+ Netzke::Draper.configure do |config|
32
+ config.decorate_by_default = false
33
+ end
34
+
35
+ c = DecoratedComponent.new
36
+ c.decorator_class.should be_false
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe Netzke::Basepack::DataAdapters::AbstractAdapter do
4
+ before(:each) do
5
+ Netzke::Draper::Configuration.reset_config
6
+ @decorated_component = DecoratedComponent.new
7
+ @undecorated_component = UndecoratedComponent.new
8
+ end
9
+
10
+ describe "#find_record" do
11
+ before(:each) do
12
+ Book.stub_chain(:where, :first).and_return(Book.new)
13
+ end
14
+
15
+ it "decorated component" do
16
+ @decorated_component.data_adapter.find_record(1).should be_a(BookDecorator)
17
+ end
18
+
19
+ it "undecorated component" do
20
+ @undecorated_component.data_adapter.find_record(1).should be_a(Book)
21
+ end
22
+ end
23
+
24
+ describe "#new_record" do
25
+ it "decorated component" do
26
+ @decorated_component.data_adapter.new_record.should be_a(BookDecorator)
27
+ end
28
+
29
+ it "undecorated component" do
30
+ @undecorated_component.data_adapter.new_record.should be_a(Book)
31
+ end
32
+ end
33
+
34
+ describe "#first" do
35
+ before(:each) do
36
+ Book.stub(:first).and_return(Book.new)
37
+ end
38
+
39
+ it "decorated component" do
40
+ @decorated_component.data_adapter.first.should be_a(BookDecorator)
41
+ end
42
+
43
+ it "undecorated component" do
44
+ @undecorated_component.data_adapter.first.should be_a(Book)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe Netzke::Basepack::DataAdapters::ActiveRecordAdapter do
4
+ before(:each) do
5
+ Netzke::Draper::Configuration.reset_config
6
+ @decorated_component = DecoratedComponent.new
7
+ @undecorated_component = UndecoratedComponent.new
8
+ end
9
+
10
+ describe "#get_records" do
11
+ before(:each) do
12
+ @decorated_component.data_adapter.stub_chain(:get_relation, :all).and_return([Book.new])
13
+ end
14
+
15
+ describe "decorated component" do
16
+ it "decorates collection" do
17
+ @decorated_component.data_adapter.get_records({}).should be_a(Draper::DecoratedEnumerableProxy)
18
+ end
19
+
20
+ it "decorates elements" do
21
+ @decorated_component.data_adapter.get_records({}).each do |element|
22
+ element.should be_a(BookDecorator)
23
+ end
24
+ end
25
+ end
26
+
27
+ describe "undecorated component" do
28
+ before(:each) do
29
+ @undecorated_component.data_adapter.stub_chain(:get_relation, :all).and_return([Book.new])
30
+ end
31
+
32
+ it "doesn't decorate collection" do
33
+ @undecorated_component.data_adapter.get_records({}).should_not be_a(Draper::DecoratedEnumerableProxy)
34
+ end
35
+
36
+ it "doesn't decorate elements" do
37
+ @undecorated_component.data_adapter.get_records({}).each do |element|
38
+ element.should be_a(Book)
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ end
@@ -0,0 +1,67 @@
1
+ require 'rubygems'
2
+ require 'rails'
3
+ require 'active_record'
4
+ require 'netzke-basepack'
5
+ require 'draper'
6
+ require 'bundler/setup'
7
+ require 'netzke-draper'
8
+ Bundler.require(:default)
9
+
10
+ require 'active_support/all'
11
+
12
+ RSpec.configure do |config|
13
+ config.treat_symbols_as_metadata_keys_with_true_values = true
14
+ config.filter_run :focus => true
15
+ config.run_all_when_everything_filtered = true
16
+ config.mock_with :rspec
17
+ end
18
+
19
+ class Book < ActiveRecord::Base
20
+ def self.columns
21
+ []
22
+ end
23
+
24
+ def self.primary_key
25
+ :id
26
+ end
27
+ end
28
+
29
+ class BookDecorator < Draper::Base
30
+ decorates :book
31
+ end
32
+
33
+ class NovelDecorator < Draper::Base
34
+ decorates :book
35
+ end
36
+
37
+ class DataAccessorComponent < Netzke::Base
38
+ include Netzke::Basepack::DataAccessor
39
+
40
+ def configure(c)
41
+ c.model = 'Book'
42
+ end
43
+ end
44
+
45
+ class DecoratedComponent < DataAccessorComponent
46
+ end
47
+
48
+ class StringDecoratorComponent < DataAccessorComponent
49
+ def configure(c)
50
+ super
51
+ c.decorator = 'NovelDecorator'
52
+ end
53
+ end
54
+
55
+ class ClassDecoratorComponent < DataAccessorComponent
56
+ def configure(c)
57
+ super
58
+ c.decorator = NovelDecorator
59
+ end
60
+ end
61
+
62
+ class UndecoratedComponent < DataAccessorComponent
63
+ def configure(c)
64
+ super
65
+ c.decorator = false
66
+ end
67
+ end
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: netzke-draper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Georg Meyer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.6.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.6.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '0.9'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '0.9'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rails
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 3.2.8
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 3.2.8
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec-rails
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 2.6.1
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 2.6.1
78
+ - !ruby/object:Gem::Dependency
79
+ name: draper
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: netzke-basepack
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Gives you the ability to use any decorator in netzke components with
111
+ almost no effort
112
+ email: georgmeyer83@googlemail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - .rspec
119
+ - Gemfile
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - init.rb
124
+ - lib/netzke-draper.rb
125
+ - lib/netzke/draper.rb
126
+ - lib/netzke/draper/basepack_ext/data_accessor.rb
127
+ - lib/netzke/draper/basepack_ext/data_adapters/abstract_adapter.rb
128
+ - lib/netzke/draper/basepack_ext/data_adapters/active_record_adapter.rb
129
+ - lib/netzke/draper/configuration.rb
130
+ - lib/netzke/draper/version.rb
131
+ - netzke-draper.gemspec
132
+ - spec/netzke/draper/basepack_ext/data_accessor_spec.rb
133
+ - spec/netzke/draper/basepack_ext/data_adapters/abstract_adapter_spec.rb
134
+ - spec/netzke/draper/basepack_ext/data_adapters/active_record_adapter_spec.rb
135
+ - spec/spec_helper.rb
136
+ homepage: http://github.com/scho/netzke-draper
137
+ licenses: []
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ! '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ segments:
149
+ - 0
150
+ hash: 766367901
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ none: false
153
+ requirements:
154
+ - - ! '>='
155
+ - !ruby/object:Gem::Version
156
+ version: 1.3.4
157
+ requirements: []
158
+ rubyforge_project:
159
+ rubygems_version: 1.8.24
160
+ signing_key:
161
+ specification_version: 3
162
+ summary: Enables draper decorators in Netzke components
163
+ test_files: []