deals_with 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,48 @@
1
+ # DealsWith
2
+
3
+ module DealsWith
4
+ module ActionController # :nodoc:
5
+ module ClassMethods
6
+
7
+ def deals_with_models
8
+ @deals_with_models ||= {}
9
+ end
10
+
11
+ def deals_with(model_name, options={}, &scope_proc)
12
+ model_name = model_name.to_s.classify
13
+ model_class = model_name.constantize
14
+
15
+ unless deals_with_models[model_name]
16
+
17
+ model_class.reflect_on_all_associations(:belongs_to).each do |reflection|
18
+ deals_with(reflection.class_name)
19
+ end
20
+
21
+ deals_with_models[model_name] = DealsWith::Resource.new(self,
22
+ model_name, options, &scope_proc)
23
+
24
+ generate_resource_methods(model_name)
25
+ end
26
+ end
27
+
28
+
29
+ def generate_resource_methods(model_name)
30
+ class_eval %{
31
+ def #{model_name.tableize}
32
+ @#{model_name.tableize} ||=
33
+ self.class.deals_with_models[#{model_name.inspect}].all(self)
34
+ end
35
+ def #{model_name.underscore}
36
+ @#{model_name.underscore} ||=
37
+ self.class.deals_with_models[#{model_name.inspect}].find(self)
38
+ end
39
+ def build_#{model_name.underscore}
40
+ @#{model_name.underscore} ||=
41
+ self.class.deals_with_models[#{model_name.inspect}].build(self)
42
+ end
43
+ }
44
+ end
45
+
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,13 @@
1
+ # DealsWith
2
+
3
+ module DealsWith
4
+ module ActiveRecord # :nodoc:
5
+ module ClassMethods
6
+
7
+ def build(attributes={})
8
+ new(attributes)
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,101 @@
1
+ # DealsWith
2
+
3
+ module DealsWith
4
+ class Resource
5
+
6
+ attr_reader :model_name
7
+
8
+ private
9
+
10
+ attr_reader :controller_class, :options, :scope_proc
11
+
12
+ public
13
+
14
+ def initialize(controller_class, model_name, options={}, &scope_proc)
15
+ @controller_class = controller_class
16
+ @model_name = model_name.to_s.classify
17
+ @options = options
18
+ @scope_proc = scope_proc
19
+ end
20
+
21
+ private
22
+
23
+ def model_class
24
+ @model_class ||= model_name.constantize
25
+ end
26
+
27
+ def parents
28
+ @parents ||= model_class.reflect_on_all_associations(:belongs_to).collect do |reflection|
29
+ if reflection.klass.instance_methods.include? model_name.tableize
30
+ controller_class.deals_with_models[reflection.class_name]
31
+ end
32
+ end.compact
33
+ end
34
+
35
+ def primary_for(controller)
36
+ controller_class.to_s.include? "#{model_name.pluralize}_controller".classify
37
+ end
38
+
39
+ def base(controller)
40
+ base = nil
41
+ parents.each do |resource|
42
+ if resource.can_find?(controller)
43
+ base = controller.send(resource.model_name.underscore)
44
+ break
45
+ end
46
+ end
47
+ base = base.send(model_name.tableize) if base
48
+ base ||= model_class
49
+ base = @scope_proc.call(base) if @scope_proc
50
+ base
51
+ end
52
+
53
+ def find_param_key(controller)
54
+ return @find_param_key if @find_param_key
55
+ if @options[:find_param]
56
+ @find_param_key = @options[:find_param]
57
+ else
58
+ @find_param_key = "#{model_name.underscore}_id"
59
+ @find_param_key = 'id' if primary_for(controller)
60
+ end
61
+ @find_param_key
62
+ end
63
+
64
+ def find_param(controller)
65
+ controller.params[find_param_key(controller)]
66
+ end
67
+
68
+ def build_param_key(controller)
69
+ @build_param_key ||= if @options[:build_param]
70
+ @options[:build_param]
71
+ else
72
+ "#{model_name.underscore}"
73
+ end
74
+ end
75
+
76
+ def build_param(controller)
77
+ controller.params[build_param_key(controller)] || {}
78
+ end
79
+
80
+ public
81
+
82
+ def can_find?(controller)
83
+ !find_param(controller).blank?
84
+ end
85
+
86
+ # searchers
87
+
88
+ def all(controller)
89
+ base(controller).all
90
+ end
91
+
92
+ def find(controller)
93
+ base(controller).find(find_param(controller))
94
+ end
95
+
96
+ def build(controller)
97
+ base(controller).build(build_param(controller))
98
+ end
99
+
100
+ end
101
+ end
data/lib/deals_with.rb ADDED
@@ -0,0 +1,4 @@
1
+ # DealsWith
2
+
3
+ module DealsWith
4
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,4 @@
1
+ # Include hook code here
2
+
3
+ ::ActionController::Base.send :extend, ::DealsWith::ActionController::ClassMethods
4
+ ::ActiveRecord::Base.send :extend, ::DealsWith::ActiveRecord::ClassMethods
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :deals_with do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class DealsWithTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deals_with
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Simon Menke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-26 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Automaticaly find any appropriately scoped resources.
17
+ email: simon.menke@gmail.com
18
+ engine_dependencies: {}
19
+
20
+ executables: []
21
+
22
+ extensions: []
23
+
24
+ extra_rdoc_files: []
25
+
26
+ files:
27
+ - lib/deals_with/action_controller/class_methods.rb
28
+ - lib/deals_with/active_record/class_methods.rb
29
+ - lib/deals_with/resource.rb
30
+ - lib/deals_with.rb
31
+ - rails/init.rb
32
+ - tasks/deals_with_tasks.rake
33
+ - test/deals_with_test.rb
34
+ - test/test_helper.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/simonmenke/deals_with
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.3.3
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Automaticaly find any appropriately scoped resources.
63
+ test_files:
64
+ - test/deals_with_test.rb
65
+ - test/test_helper.rb