resourceize 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest ADDED
@@ -0,0 +1,10 @@
1
+ Manifest
2
+ README.textile
3
+ Rakefile
4
+ init.rb
5
+ lib/resourceize.rb
6
+ lib/resourceize/before.rb
7
+ lib/resourceize/instance_variables.rb
8
+ lib/resourceize/resources_controller.rb
9
+ lib/resourceize/routing_methods.rb
10
+ resourceize.gemspec
data/README.textile ADDED
@@ -0,0 +1,13 @@
1
+ h1. Resourceize
2
+
3
+ RESTful controllers and VIEWS with no code!
4
+
5
+ README coming soon..
6
+
7
+ h2. Project Details
8
+
9
+ h3. Roadmap
10
+ * Add @ObjectFormatter@ module
11
+
12
+ Copyright © 2010 Maurizio Casimirri, released under the LGPL license.
13
+
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('resourceize', '0.1.1') do |p|
6
+ p.description = "RESTful controllers and views without coding"
7
+ p.url = "http://github.com/mcasimir/resourceize"
8
+ p.author = "Maurizio Casimirri"
9
+ p.email = "maurizio.cas@gmail.com"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = ["requested"]
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
15
+
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'resourceize'
2
+
@@ -0,0 +1,22 @@
1
+ require 'requested'
2
+
3
+ # Author:: Maurizio Casimirri (mailto:maurizio.cas@gmail.com)
4
+ # Copyright:: Copyright (c) 2010 Maurizio Casimirri
5
+
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+
11
+ # This library is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ # Lesser General Public License for more details.
15
+
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+
20
+ module Resourceize
21
+ end
22
+
@@ -0,0 +1,38 @@
1
+ # Author:: Maurizio Casimirri (mailto:maurizio.cas@gmail.com)
2
+ # Copyright:: Copyright (c) 2010 Maurizio Casimirri
3
+
4
+ # This library is free software; you can redistribute it and/or
5
+ # modify it under the terms of the GNU Lesser General Public
6
+ # License as published by the Free Software Foundation; either
7
+ # version 2.1 of the License, or (at your option) any later version.
8
+
9
+ # This library is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ # Lesser General Public License for more details.
13
+
14
+ # You should have received a copy of the GNU Lesser General Public
15
+ # License along with this library; if not, write to the Free Software
16
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
+
18
+ module Resourceize
19
+ module Before
20
+ def self.included(base)
21
+ base.extend ClassMethods
22
+ end
23
+
24
+ module ClassMethods
25
+ def before(action_or_array, &block)
26
+ action_or_array = [action_or_array] if !action_or_array.is_a? Array
27
+
28
+ actions = action_or_array
29
+
30
+ self.send(:before_filter, :only => actions) do |controller|
31
+ controller.instance_eval(&block)
32
+ end
33
+ end
34
+ end
35
+
36
+ end
37
+ end
38
+
@@ -0,0 +1,139 @@
1
+ # Author:: Maurizio Casimirri (mailto:maurizio.cas@gmail.com)
2
+ # Copyright:: Copyright (c) 2010 Maurizio Casimirri
3
+
4
+ # This library is free software; you can redistribute it and/or
5
+ # modify it under the terms of the GNU Lesser General Public
6
+ # License as published by the Free Software Foundation; either
7
+ # version 2.1 of the License, or (at your option) any later version.
8
+
9
+ # This library is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ # Lesser General Public License for more details.
13
+
14
+ # You should have received a copy of the GNU Lesser General Public
15
+ # License along with this library; if not, write to the Free Software
16
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
+
18
+ module Resourceize
19
+ module InstanceVariables
20
+
21
+ def self.included(base)
22
+
23
+ base.send(:before_filter) do |controller|
24
+
25
+ controller.instance_eval do
26
+ if request.resource
27
+
28
+ if request.action.collection?
29
+
30
+ if request.resource.respond_to? :paginate
31
+ @collection = request.resource.paginate({:page => params[:page]})
32
+ else
33
+ @collection = request.resource.all
34
+ end
35
+
36
+ mapper = :active_record if (defined?(ActiveRecord) && request.resource.superclass == ActiveRecord::Base) # TODO: WALK THE ENTIRE INHERITANCE CHAIN!!!
37
+ mapper = :mongo_mapper if (defined?(MongoMapper) && request.resource.include?(MongoMapper::Document))
38
+
39
+ keys = if mapper == :active_record
40
+ request.resource.class.column_names
41
+ elsif mapper == :mongo_mapper
42
+ request.resource.keys.keys.map &:to_s
43
+ end
44
+
45
+ @column_names = keys
46
+
47
+ instance_variable_set :"@#{request.collection_name}", @collection
48
+
49
+ elsif request.action.single_object?
50
+
51
+ # single object request.actions
52
+ if request.action.new?
53
+ @object = request.resource.new
54
+
55
+ elsif request.action.create?
56
+ @object = request.resource.new(params[:"#{request.resource_name}"])
57
+
58
+ else
59
+
60
+ begin
61
+ @object = request.resource.find(params[:id])
62
+ rescue
63
+ @object = request.resource.find_by_permalink(params[:id])
64
+ end
65
+
66
+ end #~ single_object requested_actions
67
+
68
+ meta = InstanceVariables.object_meta(@object)
69
+
70
+ @label = meta[:label]
71
+ @label_method = meta[:label_method]
72
+ @column_names = meta[:column_names]
73
+ @title = meta[:title]
74
+ @description = meta[:description]
75
+ @keywords = meta[:keywords]
76
+
77
+ instance_variable_set :"@#{request.resource_name}", @object
78
+
79
+ end #~ if elsif
80
+
81
+ end #~ if request.resource
82
+ end #~ instance_eval
83
+
84
+ end #~ send :before_filter
85
+
86
+ end
87
+
88
+
89
+ def InstanceVariables.object_meta(object)
90
+
91
+ mapper = :active_record if (defined?(ActiveRecord) && object.is_a?(ActiveRecord::Base))
92
+ mapper = :mongo_mapper if (defined?(MongoMapper) && object.class.include?(MongoMapper::Document))
93
+
94
+ keys = if mapper == :active_record
95
+ object.class.column_names
96
+ elsif mapper == :mongo_mapper
97
+ object.class.keys.keys.map &:to_s
98
+ end
99
+
100
+ label_method = if object.respond_to?(:to_label)
101
+ "to_label"
102
+ elsif object.respond_to? "label"
103
+ "label"
104
+ elsif keys.include? "name"
105
+ "name"
106
+ elsif keys.include? "title"
107
+ "title"
108
+ end
109
+
110
+ label = (object.send(:"#{label_method}") if label_method) || ""
111
+ title = label
112
+
113
+ description = if keys.include? "excerpt"
114
+ object.excerpt
115
+ elsif keys.include? "description"
116
+ object.description
117
+ end
118
+
119
+ keywords = if object.respond_to? :"keywords"
120
+ object.keywords
121
+ elsif object.respond_to? :"tag_list"
122
+ object.tag_list
123
+ end
124
+
125
+ {
126
+ :label => label,
127
+ :label_method => label_method,
128
+ :column_names => keys,
129
+ :title => title,
130
+ :description => description,
131
+ :keywords => keywords
132
+ }
133
+
134
+ end
135
+
136
+
137
+ end
138
+ end
139
+
@@ -0,0 +1,174 @@
1
+ # Author:: Maurizio Casimirri (mailto:maurizio.cas@gmail.com)
2
+ # Copyright:: Copyright (c) 2010 Maurizio Casimirri
3
+
4
+ # This library is free software; you can redistribute it and/or
5
+ # modify it under the terms of the GNU Lesser General Public
6
+ # License as published by the Free Software Foundation; either
7
+ # version 2.1 of the License, or (at your option) any later version.
8
+
9
+ # This library is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ # Lesser General Public License for more details.
13
+
14
+ # You should have received a copy of the GNU Lesser General Public
15
+ # License along with this library; if not, write to the Free Software
16
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
+
18
+
19
+ module Resourceize
20
+ class ResourcesController < ActionController::Base
21
+ include Resourceize::Before
22
+ include Resourceize::InstanceVariables
23
+ include Resourceize::RoutingMethods
24
+
25
+
26
+ helper_method :extra_object_actions, :extra_collection_actions, :extra_collection_columns
27
+
28
+ def self.set_resource(resource_class)
29
+ request.instance_eval {@resource = resource_class}
30
+ end
31
+
32
+ def self.follow(action, &block)
33
+ action = :"#{action}"
34
+ @follow ||= {}
35
+ @follow[action] = block
36
+ end
37
+
38
+ #
39
+ def new
40
+ render :template => template_for("new")
41
+ end
42
+
43
+ #
44
+ def edit
45
+ render :template => template_for("edit")
46
+ end
47
+
48
+ #
49
+ def index
50
+ render :template => template_for("index")
51
+ end
52
+
53
+ #
54
+ def create
55
+ if @object.save
56
+ success :redirect_to => follow_action(:create) || (can_render_show? ? object_path(@object) : collection_path)
57
+ else
58
+ failure :render => template_for("new")
59
+ end
60
+ end
61
+
62
+ #
63
+ def update
64
+ if @object.update_attributes(params[:"#{request.resource_name}"])
65
+ success :redirect_to => follow_action(:update) || (can_render_show? ? object_path(@object) : collection_path)
66
+ else
67
+ failure :render => template_for("edit")
68
+ end
69
+ end
70
+
71
+ #
72
+ def destroy
73
+ if @object.destroy
74
+ success :redirect_to => follow_action(:destroy) || collection_path
75
+ else
76
+ failure :redirect_to => collection_path
77
+ end
78
+ end
79
+
80
+ #
81
+ def show
82
+
83
+ end
84
+
85
+ protected
86
+ def extra_object_actions(object)
87
+ []
88
+ end
89
+
90
+ def extra_collection_actions
91
+ []
92
+ end
93
+
94
+ def extra_collection_columns
95
+ []
96
+ end
97
+
98
+ private
99
+
100
+ #
101
+ def follow_action(action)
102
+ follow_hash = self.class.instance_eval {@follow}
103
+ block = follow_hash[action] if follow_hash
104
+ self.instance_eval(&block) if block
105
+ end
106
+
107
+ #
108
+ def can_render_show?
109
+ File.exists?(File.join(Rails.root, "app", "views", controller_name, "show.html.erb"))
110
+ end
111
+
112
+ #
113
+ def template_for(action)
114
+ custom_template = File.join(Rails.root, "app", "views", request.controller, "#{action}.html.erb")
115
+ if File.exists?(custom_template)
116
+ "#{request.controller}/#{action}"
117
+ else
118
+ "default_templates/#{action}"
119
+ end
120
+ end
121
+
122
+ #
123
+ def success(options)
124
+
125
+ last_action = {}
126
+ last_action[:action] = request.action
127
+ last_action[:success] = true
128
+ last_action[:resource_class] = request.resource_name
129
+ last_action[:resource_id] = params[:id]
130
+ last_action[:resource_title] = @label
131
+
132
+ flash[:last_action] = last_action
133
+
134
+ respond_to do |wants|
135
+ wants.html {
136
+ if options[:redirect_to]
137
+ redirect_to(options[:redirect_to])
138
+ else
139
+ render(:template => options[:render])
140
+ end
141
+ }
142
+
143
+ wants.js {render :json => true.to_json}
144
+ end
145
+ end
146
+
147
+ #
148
+ def failure(options)
149
+
150
+ last_action = {}
151
+ last_action[:action] = request.action
152
+ last_action[:success] = false
153
+ last_action[:resource_class] = request.resource_name
154
+ last_action[:resource_id] = params[:id]
155
+ last_action[:resource_title] = @label
156
+
157
+ flash[:last_action] = last_action
158
+
159
+ respond_to do |wants|
160
+ wants.html {
161
+ if options[:redirect_to]
162
+ redirect_to(options[:redirect_to])
163
+ else
164
+ render(:template => options[:render])
165
+ end
166
+ }
167
+ wants.js {head :internal_server_error}
168
+ end
169
+
170
+ end
171
+
172
+ end
173
+ end
174
+
@@ -0,0 +1,76 @@
1
+ # Author:: Maurizio Casimirri (mailto:maurizio.cas@gmail.com)
2
+ # Copyright:: Copyright (c) 2010 Maurizio Casimirri
3
+
4
+ # This library is free software; you can redistribute it and/or
5
+ # modify it under the terms of the GNU Lesser General Public
6
+ # License as published by the Free Software Foundation; either
7
+ # version 2.1 of the License, or (at your option) any later version.
8
+
9
+ # This library is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ # Lesser General Public License for more details.
13
+
14
+ # You should have received a copy of the GNU Lesser General Public
15
+ # License along with this library; if not, write to the Free Software
16
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
+
18
+ module Resourceize
19
+ module RoutingMethods
20
+
21
+ def self.included(base)
22
+
23
+ base.send :include, InstanceMethods
24
+
25
+ base.send :helper_method, :object_path
26
+ base.send :helper_method, :edit_object_path
27
+ base.send :helper_method, :new_object_path
28
+ base.send :helper_method, :collection_path
29
+ base.send :helper_method, :object_url
30
+ base.send :helper_method, :edit_object_url
31
+ base.send :helper_method, :new_object_url
32
+ base.send :helper_method, :collection_url
33
+
34
+ end
35
+
36
+
37
+ module InstanceMethods
38
+ protected
39
+
40
+ def object_path(object)
41
+ self.send :"#{request.resource_name}_path", object
42
+ end
43
+
44
+ def edit_object_path(object)
45
+ self.send :"edit_#{request.resource_name}_path", object
46
+ end
47
+
48
+ def new_object_path
49
+ self.send :"new_#{request.resource_name}_path"
50
+ end
51
+
52
+ def collection_path
53
+ self.send :"#{request.collection_name}_path"
54
+ end
55
+
56
+ def object_url(object)
57
+ self.send :"#{request.resource_name}_url", object
58
+ end
59
+
60
+ def edit_object_url(object)
61
+ self.send :"edit_#{request.resource_name}_url", object
62
+ end
63
+
64
+ def new_object_url
65
+ self.send :"new_#{request.resource_name}_url"
66
+ end
67
+
68
+ def collection_url
69
+ self.send :"#{request.collection_name}_url"
70
+ end
71
+
72
+ end
73
+
74
+ end
75
+ end
76
+
@@ -0,0 +1,33 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{resourceize}
5
+ s.version = "0.1.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Maurizio Casimirri"]
9
+ s.date = %q{2010-07-19}
10
+ s.description = %q{RESTful controllers and views without coding}
11
+ s.email = %q{maurizio.cas@gmail.com}
12
+ s.extra_rdoc_files = ["README.textile", "lib/resourceize.rb", "lib/resourceize/before.rb", "lib/resourceize/instance_variables.rb", "lib/resourceize/resources_controller.rb", "lib/resourceize/routing_methods.rb"]
13
+ s.files = ["Manifest", "README.textile", "Rakefile", "init.rb", "lib/resourceize.rb", "lib/resourceize/before.rb", "lib/resourceize/instance_variables.rb", "lib/resourceize/resources_controller.rb", "lib/resourceize/routing_methods.rb", "resourceize.gemspec"]
14
+ s.homepage = %q{http://github.com/mcasimir/resourceize}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Resourceize", "--main", "README.textile"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{resourceize}
18
+ s.rubygems_version = %q{1.3.7}
19
+ s.summary = %q{RESTful controllers and views without coding}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
+ s.add_development_dependency(%q<requested>, [">= 0"])
27
+ else
28
+ s.add_dependency(%q<requested>, [">= 0"])
29
+ end
30
+ else
31
+ s.add_dependency(%q<requested>, [">= 0"])
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resourceize
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
+ platform: ruby
12
+ authors:
13
+ - Maurizio Casimirri
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-07-19 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: requested
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: RESTful controllers and views without coding
36
+ email: maurizio.cas@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.textile
43
+ - lib/resourceize.rb
44
+ - lib/resourceize/before.rb
45
+ - lib/resourceize/instance_variables.rb
46
+ - lib/resourceize/resources_controller.rb
47
+ - lib/resourceize/routing_methods.rb
48
+ files:
49
+ - Manifest
50
+ - README.textile
51
+ - Rakefile
52
+ - init.rb
53
+ - lib/resourceize.rb
54
+ - lib/resourceize/before.rb
55
+ - lib/resourceize/instance_variables.rb
56
+ - lib/resourceize/resources_controller.rb
57
+ - lib/resourceize/routing_methods.rb
58
+ - resourceize.gemspec
59
+ has_rdoc: true
60
+ homepage: http://github.com/mcasimir/resourceize
61
+ licenses: []
62
+
63
+ post_install_message:
64
+ rdoc_options:
65
+ - --line-numbers
66
+ - --inline-source
67
+ - --title
68
+ - Resourceize
69
+ - --main
70
+ - README.textile
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ hash: 3
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 11
88
+ segments:
89
+ - 1
90
+ - 2
91
+ version: "1.2"
92
+ requirements: []
93
+
94
+ rubyforge_project: resourceize
95
+ rubygems_version: 1.3.7
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: RESTful controllers and views without coding
99
+ test_files: []
100
+