qm-acts-as-generic-controller 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ All portions Copyright (c) 2007 Tim Harper
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.rdoc ADDED
@@ -0,0 +1,7 @@
1
+ =QUI: tabs
2
+
3
+ Gem for rails to create nice tabs with a few lines o code.
4
+
5
+ See: {http://qui.saepia.net}[http://qui.saepia.net] for details.
6
+
7
+ Copyright (c) 2010 [Marcin Lewandowski], released under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ require 'rake'
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |gemspec|
6
+ gemspec.name = "qm-acts-as-generic-controller"
7
+ gemspec.summary = "qM: generates generic controller actions and views"
8
+ gemspec.description = "qM: generates generic controller actions and views"
9
+ gemspec.email = "marcin@saepia.net"
10
+ gemspec.homepage = "http://q.saepia.net"
11
+ gemspec.authors = ["Marcin Lewandowski"]
12
+ gemspec.version = "0.0.4"
13
+ gemspec.files = Rake::FileList.new [ "MIT-LICENSE", "Rakefile", "lib/*", "app/views/generic_controller/*" ]
14
+ gemspec.add_dependency "qui-common-helpers", ">= 0.0.3"
15
+ gemspec.add_dependency "rfc822"
16
+ gemspec.add_dependency "sort_alphabetical"
17
+ end
18
+ rescue LoadError
19
+ puts "Jeweler not available. Install it with: sudo gem install jeweler"
20
+ end
@@ -0,0 +1,26 @@
1
+ <%- if field.is_a? Hash -%>
2
+ <%- attribute_name = field.keys.first.to_sym -%>
3
+ <%- else -%>
4
+ <%- attribute_name = field.to_sym -%>
5
+ <%- end -%>
6
+
7
+ <%- data = record.send attribute_name -%>
8
+
9
+ <%- if data.is_a?(ActiveRecord::Base) -%>
10
+ <%= link_to_object record.send(field) %>
11
+
12
+ <%- elsif data.is_a?(Array) and data.first.is_a?(ActiveRecord::Base) -%>
13
+ <%= data.collect{ |x| link_to_object(x) }.sort_alphabetical.join(", ") %>
14
+
15
+ <%- elsif field.is_a?(Hash) and field.values.first == :link_to_show -%>
16
+ <%= link_to_object record, data %>
17
+
18
+ <%- elsif field.is_a?(Hash) and field.values.first == :enum -%>
19
+ <%= h record.enum_human_value attribute_name %>
20
+
21
+ <%- elsif data.is_a?(String) and data =~ RFC822::EMAIL_REGEXP_WHOLE -%>
22
+ <%= link_to data, "mailto:#{data}" %>
23
+
24
+ <%- else -%>
25
+ <%= h data %>
26
+ <%- end -%>
@@ -0,0 +1,56 @@
1
+ <%- if defined?(section) -%>
2
+ <%- form_for_param = [ section, instance_variable_get("@#{@controller.class.to_s.demodulize.gsub("Controller", "").tableize.singularize}") ] -%>
3
+ <%- else -%>
4
+ <%- form_for_param = instance_variable_get("@#{@controller.class.to_s.demodulize.gsub("Controller", "").tableize.singularize}") -%>
5
+ <%- end -%>
6
+
7
+ <%- klass = @controller.class.to_s.demodulize.gsub("Controller", "").singularize.constantize -%>
8
+ <%- table_name = "#{@controller.class.to_s.demodulize.gsub("Controller", "").tableize.singularize}" -%>
9
+ <%- form_for form_for_param do |f| -%>
10
+ <%- unless klass.generic_form_fieldsets -%>
11
+ <fieldset>
12
+ <%- end -%>
13
+
14
+ <ol>
15
+
16
+ <%- klass.generic_fields.each do |field| -%>
17
+ <%- if field.is_a? Hash -%>
18
+ <%- attribute_name = field.keys.first.to_sym -%>
19
+ <%- else -%>
20
+ <%- attribute_name = field.to_sym -%>
21
+ <%- end -%>
22
+
23
+ <li>
24
+ <%= f.label attribute_name %>
25
+
26
+ <%- if field.is_a?(Hash) and field.values.first == :enum -%>
27
+ <%= f.select attribute_name, t("activerecord.enums.#{table_name}.#{attribute_name}").collect{|k,v| [v, k.to_s] }.sort{|x,y| x.last.to_i <=> y.last.to_i }, { :selected => params[table_name] ? params[table_name][attribute_name].to_s : f.object.send(attribute_name).to_s } %>
28
+
29
+ <%- elsif klass.generic_field_associations[attribute_name] -%>
30
+ <%- if klass.generic_field_associations[attribute_name][:kind] == :belongs_to -%>
31
+ <%= f.select attribute_name, options_from_collection_for_select(klass.generic_field_associations[attribute_name][:class_name].all, :id, :link_default_description, params[table_name] && params[table_name][attribute_name].to_i), {}, { :name => "#{table_name}[#{klass.generic_field_associations[attribute_name][:foreign_key]}]" } %>
32
+ <%- else -%>
33
+
34
+ <%- end -%>
35
+
36
+ <%- else -%>
37
+ <%= f.text_field attribute_name %>
38
+ <%- end -%>
39
+ </li>
40
+ <%- end -%>
41
+
42
+ </ol>
43
+
44
+ <%- unless klass.generic_form_fieldsets -%>
45
+ </fieldset>
46
+ <%- end -%>
47
+
48
+
49
+
50
+ <%- if defined?(nice_submit_tag) -%>
51
+ <%= nice_submit_tag f, :cancel => :index %>
52
+ <%- else -%>
53
+ <%= f.submit %>
54
+ <%- end -%>
55
+ <%- end -%>
56
+
@@ -0,0 +1,30 @@
1
+ <%- if defined?(QUI::Toolbar) -%>
2
+ <%- toolbar do |t| -%>
3
+ <%= t.create %>
4
+ <%- end -%>
5
+ <%- end -%>
6
+
7
+ <%- if defined?(QUI::IndexTable) -%>
8
+ <%- klass = @controller.class.to_s.demodulize.gsub("Controller", "").singularize.constantize -%>
9
+ <%- headers = klass.generic_fields.collect{|x| x.is_a?(Hash) ? ".#{x.keys.first}".to_sym : ".#{x}".to_sym} -%>
10
+ <%- has_edit = ActionController::Routing::Routes.routes.collect{|x| x if x.matches_controller_and_action?(@controller.class.to_s.gsub("Controller", "").tableize, "edit") }.compact.size > 0 %>
11
+ <%- has_destroy = ActionController::Routing::Routes.routes.collect{|x| x if x.matches_controller_and_action?(@controller.class.to_s.gsub("Controller", "").tableize, "destroy") }.compact.size > 0 %>
12
+
13
+ <%- headers << :oneicon if has_edit %>
14
+ <%- headers << :oneicon if has_destroy %>
15
+
16
+ <%- index_table instance_variable_get("@#{@controller.class.to_s.demodulize.gsub("Controller", "").tableize}"), :headers => headers, :class_name => klass do |r| %>
17
+ <%- klass.generic_fields.each do |field| -%>
18
+ <%- if field.is_a? Hash -%>
19
+ <%- attribute_name = field.keys.first.to_sym -%>
20
+ <%- else -%>
21
+ <%- attribute_name = field.to_sym -%>
22
+ <%- end -%>
23
+
24
+ <td><%= render :partial => "generic_controller/data", :locals => { :record => r, :field => field } %></td>
25
+ <%- end -%>
26
+
27
+ <td><%= link_to_edit r if has_edit %></td>
28
+ <td><%= link_to_delete r if has_destroy %></td>
29
+ <%- end -%>
30
+ <%- end -%>
@@ -0,0 +1,16 @@
1
+ <%- klass = @controller.class.to_s.demodulize.gsub("Controller", "").singularize.constantize -%>
2
+ <%- table_name = "#{@controller.class.to_s.demodulize.gsub("Controller", "").tableize.singularize}" -%>
3
+ <%- record = instance_variable_get("@#{@controller.class.to_s.demodulize.gsub("Controller", "").tableize.singularize}") -%>
4
+ <dl>
5
+ <%- klass.generic_fields.each do |field| -%>
6
+ <%- if field.is_a? Hash -%>
7
+ <%- attribute_name = field.keys.first.to_sym -%>
8
+ <%- else -%>
9
+ <%- attribute_name = field.to_sym -%>
10
+ <%- end -%>
11
+
12
+ <dt><%= h klass.human_attribute_name attribute_name %></dt>
13
+
14
+ <dd><%= render :partial => "generic_controller/data", :locals => { :record => record, :field => field } %></dd>
15
+ <%- end -%>
16
+ </dl>
@@ -0,0 +1,135 @@
1
+ module QM
2
+ module ActsAsGenericController
3
+ module ControllerIncludes
4
+ def self.included(base) # :nodoc:
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def acts_as_generic_controller
10
+ include InstanceMethods
11
+
12
+ class_eval <<-END
13
+ rescue_from ActionView::MissingTemplate do |exception|
14
+ case params[:action].to_sym
15
+ when :index
16
+ render :template => "generic_controller/index"
17
+
18
+ when :new, :edit
19
+ render :template => "generic_controller/form"
20
+
21
+ when :show
22
+ render :template => "generic_controller/show"
23
+
24
+ else
25
+ raise exception
26
+ end
27
+ end
28
+ END
29
+
30
+ end
31
+ end
32
+
33
+ module InstanceMethods
34
+ def index
35
+ instance_variable_set(plural_variable, model.all) unless instance_variable_defined?(plural_variable)
36
+
37
+ respond_to do |format|
38
+ format.html
39
+ format.xml { render :xml => instance_variable_get(plural_variable) }
40
+ end
41
+ end
42
+
43
+ def show
44
+ instance_variable_set(singular_variable, model.find(params[:id])) unless instance_variable_defined?(singular_variable)
45
+
46
+ respond_to do |format|
47
+ format.html
48
+ format.xml { render :xml => instance_variable_get(singular_variable) }
49
+ end
50
+ end
51
+
52
+ def edit
53
+ instance_variable_set(singular_variable, model.find(params[:id])) unless instance_variable_defined?(singular_variable)
54
+ end
55
+
56
+ def new
57
+ instance_variable_set singular_variable, model.new unless instance_variable_defined?(singular_variable)
58
+
59
+ respond_to do |format|
60
+ format.html
61
+ format.xml { render :xml => instance_variable_get(singular_variable) }
62
+ end
63
+ end
64
+
65
+ def create
66
+ instance_variable_set(singular_variable, model.new) unless instance_variable_defined?(singular_variable)
67
+ instance_variable_get(singular_variable).update_attributes params[singular_variable(true)]
68
+
69
+ respond_to do |format|
70
+ if instance_variable_get(singular_variable).save
71
+ flash[:notice] = :savedAsFlash
72
+ if defined?(:section)
73
+ format.html { redirect_to [ section, instance_variable_get(singular_variable) ] }
74
+ format.xml { render :xml => instance_variable_get(singular_variable), :status => :created, :location => [ section, instance_variable_get(singular_variable) ] }
75
+ else
76
+ format.html { redirect_to instance_variable_get(singular_variable) }
77
+ format.xml { render :xml => instance_variable_get(singular_variable), :status => :created, :location => instance_variable_get(singular_variable) }
78
+ end
79
+ else
80
+ format.html { render :template => "generic_controller/form" }
81
+ format.xml { render :xml => instance_variable_get(singular_variable), :status => :unprocessable_entity }
82
+ end
83
+ end
84
+ end
85
+
86
+ def update
87
+ instance_variable_set(singular_variable, model.find(params[:id])) unless instance_variable_defined?(singular_variable)
88
+ instance_variable_get(singular_variable).update_attributes params[singular_variable(true)]
89
+
90
+ respond_to do |format|
91
+ if instance_variable_get(singular_variable).save
92
+ flash[:notice] = :savedAsFlash
93
+ if defined?(:section)
94
+ format.html { redirect_to [ section, instance_variable_get(singular_variable) ] }
95
+ format.xml { head :ok }
96
+ else
97
+ format.html { redirect_to instance_variable_get(singular_variable) }
98
+ format.xml { head :ok }
99
+ end
100
+ else
101
+ format.html { render :template => "generic_controller/form" }
102
+ format.xml { render :xml => instance_variable_get(singular_variable), :status => :unprocessable_entity }
103
+ end
104
+ end
105
+ end
106
+
107
+ def destroy
108
+ instance_variable_set(singular_variable, model.find(params[:id])) unless instance_variable_defined?(singular_variable)
109
+ instance_variable_get(singular_variable).destroy
110
+
111
+ flash[:notice] = :destroyedAsFlash
112
+
113
+ respond_to do |format|
114
+ format.html { redirect_to :action => "index" }
115
+ format.xml { head :ok }
116
+ end
117
+ end
118
+
119
+ protected
120
+ def model
121
+ self.class.to_s.demodulize.gsub("Controller", "").singularize.constantize
122
+ end
123
+
124
+ def plural_variable(local = false)
125
+ :"#{(local ? "" : "@")}#{self.class.to_s.demodulize.gsub("Controller", "").tableize}"
126
+ end
127
+
128
+ def singular_variable(local = false)
129
+ :"#{(local ? "" : "@")}#{self.class.to_s.demodulize.gsub("Controller", "").tableize.singularize}"
130
+ end
131
+
132
+ end
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,56 @@
1
+ module QM
2
+ module ActsAsGenericController
3
+ module ModelIncludes
4
+ def self.included(base) # :nodoc:
5
+ base.extend ClassMethods
6
+
7
+ end
8
+
9
+ module ClassMethods
10
+ def generic_field_associations
11
+ @generic_field_associations
12
+ end
13
+
14
+ def has_many(association_id, options = {}, &extension)
15
+ @generic_field_associations ||= {}
16
+ @generic_field_associations[association_id.to_sym] = { :kind => :has_many, :class_name => ( options[:class_name] ? options[:class_name].constantize : association_id.to_s.classify.constantize ) }
17
+
18
+ super association_id, options, &extension
19
+ end
20
+
21
+ def has_and_belongs_to_many(association_id, options = {}, &extension)
22
+ @generic_field_associations ||= {}
23
+ @generic_field_associations[association_id.to_sym] = { :kind => :has_and_belongs_to_many, :class_name => ( options[:class_name] ? options[:class_name].constantize : association_id.to_s.classify.constantize ) }
24
+
25
+ super association_id, options, &extension
26
+ end
27
+
28
+ def belongs_to(association_id, options = {})
29
+ @generic_field_associations ||= {}
30
+ klass = ( options[:class_name].constantize ? options[:class_name].constantize : association_id.to_s.classify.constantize )
31
+ @generic_field_associations[association_id.to_sym] = { :kind => :belongs_to, :class_name => klass, :foreign_key => options[:foreign_key] || klass.to_s.tableize }
32
+
33
+ super association_id, options
34
+ end
35
+
36
+
37
+
38
+ def generic_fields
39
+ @generic_fields
40
+ end
41
+
42
+ def generic_form_fieldsets
43
+ @generic_form_fieldsets
44
+ end
45
+
46
+ def has_generic_fields(*fields)
47
+ @generic_fields = fields.flatten
48
+ end
49
+
50
+ def has_generic_form_fieldsets(*fieldsets)
51
+ @generic_form_fieldsets = fieldsets.flatten
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,5 @@
1
+ require 'qm-acts-as-generic-controller-controller.rb'
2
+ require 'qm-acts-as-generic-controller-model.rb'
3
+
4
+ ActiveRecord::Base.send :include, QM::ActsAsGenericController::ModelIncludes
5
+ ActionController::Base.send :include, QM::ActsAsGenericController::ControllerIncludes
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qm-acts-as-generic-controller
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 4
10
+ version: 0.0.4
11
+ platform: ruby
12
+ authors:
13
+ - Marcin Lewandowski
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-22 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: qui-common-helpers
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 25
30
+ segments:
31
+ - 0
32
+ - 0
33
+ - 3
34
+ version: 0.0.3
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rfc822
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :runtime
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: sort_alphabetical
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ type: :runtime
64
+ version_requirements: *id003
65
+ description: "qM: generates generic controller actions and views"
66
+ email: marcin@saepia.net
67
+ executables: []
68
+
69
+ extensions: []
70
+
71
+ extra_rdoc_files:
72
+ - README.rdoc
73
+ files:
74
+ - MIT-LICENSE
75
+ - Rakefile
76
+ - app/views/generic_controller/_data.erb
77
+ - app/views/generic_controller/form.erb
78
+ - app/views/generic_controller/index.erb
79
+ - app/views/generic_controller/show.erb
80
+ - lib/qm-acts-as-generic-controller-controller.rb
81
+ - lib/qm-acts-as-generic-controller-model.rb
82
+ - lib/qm-acts-as-generic-controller.rb
83
+ - README.rdoc
84
+ has_rdoc: true
85
+ homepage: http://q.saepia.net
86
+ licenses: []
87
+
88
+ post_install_message:
89
+ rdoc_options:
90
+ - --charset=UTF-8
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ hash: 3
108
+ segments:
109
+ - 0
110
+ version: "0"
111
+ requirements: []
112
+
113
+ rubyforge_project:
114
+ rubygems_version: 1.3.7
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: "qM: generates generic controller actions and views"
118
+ test_files: []
119
+