brentano 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,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in brentano.gemspec
4
+ gemspec
@@ -0,0 +1,3 @@
1
+ <img src="http://i.imgur.com/mNV9T.jpg"/>
2
+
3
+ Brentano helps you present ActiveRecord (and other) collections to your controllers.
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "brentano/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "brentano"
7
+ s.version = Brentano::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Roberto Thais"]
10
+ s.email = ["roberto.n.thais@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Classy presenters for Rails 3}
13
+ s.description = %q{Brentano helps you present ActiveRecord collections to your controllers and avoid repetitive ORM logic}
14
+
15
+ s.rubyforge_project = "brentano"
16
+
17
+ s.add_dependency "activesupport", "~> 3.0"
18
+ s.add_dependency "activerecord", "~> 3.0"
19
+ s.add_dependency "actionpack", "~> 3.0"
20
+ s.add_dependency "hashie", "~> 0.4"
21
+
22
+ s.files = `git ls-files`.split("\n")
23
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
24
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
25
+ s.require_paths = ["lib"]
26
+ end
@@ -0,0 +1,21 @@
1
+ require 'brentano/rails'
2
+
3
+ module Brentano
4
+
5
+ autoload :Presenter, 'brentano/presenter'
6
+ autoload :Query, 'brentano/query'
7
+
8
+ mattr_accessor :query_limit
9
+ @@query_limit = 20
10
+
11
+ mattr_accessor :qualifier_key
12
+ @@qualifier_key = :options
13
+
14
+ mattr_accessor :presenter_module
15
+ @@presenter_module = "Presenters"
16
+
17
+ def config
18
+ yield self
19
+ end
20
+
21
+ end
@@ -0,0 +1,58 @@
1
+ module Brentano
2
+ class Presenter
3
+
4
+ class_attribute :sizes
5
+
6
+ attr_reader :subject, :options, :collection, :maximum_size
7
+
8
+ alias_method :proxy_respond_to?, :respond_to?
9
+
10
+ def initialize(subject, options, &blk)
11
+ @subject = subject
12
+ @options = Hashie::Mash.new(options)
13
+ yield self if block_given?
14
+ finish
15
+ end
16
+
17
+ def finish(&blk)
18
+ @collection = Brentano::Query.finish relation, options.dup, &blk
19
+ end
20
+
21
+ def maximum_size=(size)
22
+ @maximum_size = if size.is_a? Fixnum
23
+ size
24
+ else
25
+ sizes[size]
26
+ end
27
+ options.limit = @maximum_size
28
+ end
29
+
30
+ def respond_to?(*args)
31
+ proxy_respond_to?(args[0]) || @collection.respond_to?(args[0])
32
+ end
33
+
34
+ def method_missing(name, *args, &blk)
35
+ if @collection.respond_to? name
36
+ result = @collection.send(name, *args, &blk)
37
+ if result.is_a? ActiveRecord::Relation
38
+ self.dup.tap do |copy|
39
+ copy.instance_eval { @collection = result }
40
+ end
41
+ else
42
+ result
43
+ end
44
+ else
45
+ super
46
+ end
47
+ end
48
+
49
+ def as_json(*args)
50
+ @collection.as_json(*args)
51
+ end
52
+
53
+ def to_sql
54
+ @collection.to_sql
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,40 @@
1
+ # A lightweight wrapper class over ActiveRecord::Relation which allows certain
2
+ # query parameters to be supplied as a hash. Good for automating and securing
3
+ # queries from request params
4
+ module Brentano
5
+ class Query
6
+
7
+ attr_accessor_with_default :allowed_methods, [:limit, :offset].freeze
8
+ attr_accessor_with_default :allowed_scopes, [].freeze
9
+ attr_accessor_with_default :absolute_limit, Brentano.query_limit
10
+
11
+ def self.finish(relation, options)
12
+ obj = new relation
13
+ yield obj if block_given?
14
+ obj.apply options
15
+ end
16
+
17
+ def apply(options)
18
+ supplied_scopes = Array.wrap(options[:scopes] || options[:scope])
19
+ supplied_scopes << "by_#{options[:by]}" if options[:by].present?
20
+ supplied_scopes.map!(&:to_sym)
21
+ scopes = allowed_scopes & supplied_scopes
22
+ scoped_relation = scopes.inject(@relation) { |r, s| r.send(s) }
23
+ if absolute_limit.present?
24
+ options[:limit] ||= absolute_limit
25
+ options[:limit] = [ options[:limit].to_i, absolute_limit ].min
26
+ end
27
+ options[:offset] &&= options[:offset].to_i
28
+ options[:limit] &&= options[:limit].to_i
29
+ allowed_methods.inject(scoped_relation) do |r, m|
30
+ r = r.send(m, options[m]) if options[m].present?
31
+ r
32
+ end
33
+ end
34
+
35
+ def initialize(relation)
36
+ @relation = relation
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,14 @@
1
+ require 'brentano/rails/helpers'
2
+
3
+ module Brentano
4
+ class Railtie < ::Rails::Railtie
5
+
6
+ initializer 'brentano.extend_action_contrller' do
7
+ ActiveSupport.on_load(:action_controller) do
8
+ self.send :include, Brentano::Helpers
9
+ end
10
+ end
11
+
12
+ end
13
+ end
14
+
@@ -0,0 +1,10 @@
1
+ module Brentano
2
+ module Helpers
3
+
4
+ def presenter(klass_name, subject, options = params[Brentano.qualifier_key], &blk)
5
+ klass = "#{Brentano.presenter_module}::#{klass_name.to_s.camelcase}".constantize
6
+ klass.new subject, options, &blk
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module Brentano
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: brentano
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Roberto Thais
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-24 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activesupport
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 3
30
+ - 0
31
+ version: "3.0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: activerecord
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 3
44
+ - 0
45
+ version: "3.0"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: actionpack
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 3
58
+ - 0
59
+ version: "3.0"
60
+ type: :runtime
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: hashie
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ~>
69
+ - !ruby/object:Gem::Version
70
+ segments:
71
+ - 0
72
+ - 4
73
+ version: "0.4"
74
+ type: :runtime
75
+ version_requirements: *id004
76
+ description: Brentano helps you present ActiveRecord collections to your controllers and avoid repetitive ORM logic
77
+ email:
78
+ - roberto.n.thais@gmail.com
79
+ executables: []
80
+
81
+ extensions: []
82
+
83
+ extra_rdoc_files: []
84
+
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - README.md
89
+ - Rakefile
90
+ - brentano.gemspec
91
+ - lib/brentano.rb
92
+ - lib/brentano/presenter.rb
93
+ - lib/brentano/query.rb
94
+ - lib/brentano/rails.rb
95
+ - lib/brentano/rails/helpers.rb
96
+ - lib/brentano/version.rb
97
+ has_rdoc: true
98
+ homepage: ""
99
+ licenses: []
100
+
101
+ post_install_message:
102
+ rdoc_options: []
103
+
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ segments:
112
+ - 0
113
+ version: "0"
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ segments:
120
+ - 0
121
+ version: "0"
122
+ requirements: []
123
+
124
+ rubyforge_project: brentano
125
+ rubygems_version: 1.3.7
126
+ signing_key:
127
+ specification_version: 3
128
+ summary: Classy presenters for Rails 3
129
+ test_files: []
130
+