qm-acts-as-generic-controller 0.0.11 → 0.0.12

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.
data/Rakefile CHANGED
@@ -9,9 +9,11 @@ begin
9
9
  gemspec.email = "marcin@saepia.net"
10
10
  gemspec.homepage = "http://q.saepia.net"
11
11
  gemspec.authors = ["Marcin Lewandowski"]
12
- gemspec.version = "0.0.11"
12
+ gemspec.version = "0.0.12"
13
13
  gemspec.files = Rake::FileList.new [ "MIT-LICENSE", "Rakefile", "lib/*", "app/views/generic_controller/*" ]
14
14
  gemspec.add_dependency "qui-common-helpers", ">= 0.0.6"
15
+ gemspec.add_dependency "qui-tabs", ">= 0.0.3"
16
+ gemspec.add_dependency "qui-toolbar", ">= 0.0.4"
15
17
  gemspec.add_dependency "rfc822"
16
18
  gemspec.add_dependency "sort_alphabetical"
17
19
  gemspec.add_dependency "calendar_date_select"
@@ -1,35 +1,3 @@
1
- <%- @mutliple_select_count ||= 0 -%>
2
-
3
- <%- if defined?(field) and defined?(record) -%>
4
- <%- if field.is_a? Hash -%>
5
- <%- attribute_name = field.keys.first.to_sym -%>
6
- <%- else -%>
7
- <%- attribute_name = field.to_sym -%>
8
- <%- end -%>
9
-
10
- <%- klass = record.class -%>
11
- <%- table_name = klass.to_s.tableize.singularize -%>
12
-
13
- <%- if kind == :check -%>
14
- <%- field_name = "#{table_name}[#{attribute_name.to_s.singularize}_ids][]" -%>
15
- <%- allow_nil = false -%>
16
-
17
- <%- elsif kind == :radio -%>
18
- <%- field_name = "#{table_name}[#{klass.generic_field_associations[attribute_name][:foreign_key]}]" -%>
19
- <%- allow_nil ||= record.column_for_attribute(klass.generic_field_associations[attribute_name][:foreign_key]).null -%>
20
- <%- end -%>
21
-
22
- <%- collection = klass.generic_field_associations[attribute_name][:class_name].all -%>
23
-
24
-
25
- <%- elsif defined?(collection) and defined?(attribute_name) -%>
26
- <%- allow_nil ||= false -%>
27
- <%- field_name = attribute_name -%>
28
-
29
- <%- else -%>
30
- <%- raise ArgumentError, "You must pass some variables as locals to generic_controller/multiple_select partial. Possibilities are: 'field' and 'record' for automatic associations, or 'collection' and 'attribute_name' just for displaying a set." -%>
31
- <%- end -%>
32
-
33
1
  <%- content_tag(:div, :class => "multiple_select", :id => "multiple_select_for_#{attribute_name}") do -%>
34
2
  <%- content_tag(:div, :class => "filter") do -%>
35
3
  <%= text_field_tag "multiple_select_filter_for_#{attribute_name}", "" %>
@@ -99,26 +67,46 @@
99
67
  <%- if kind == :check -%>
100
68
  <%- if defined?(record) -%>
101
69
  <%- selected = (params[table_name] && params[table_name]["#{attribute_name.to_s.singularize}_ids"] ? params[table_name]["#{attribute_name.to_s.singularize}_ids"].include?(element.id.to_s) : record.send("#{attribute_name.to_s.singularize}_ids").include?(element.id) ) -%>
102
- <%- else -%>
70
+ <%- elsif mode == :activerecord -%>
103
71
  <%- selected = params[table_name] && params[table_name].include?(element.id.to_s) -%>
72
+ <%- else -%>
73
+ <%- selected = false -%>
104
74
  <%- end -%>
105
75
 
106
- <%= check_box_tag field_name, element.id, selected, :id => "ms#{@mutliple_select_count}_#{table_name}_#{attribute_name}_#{element.id}" %>
76
+ <%- if mode == :collection and element.is_a? Array -%>
77
+ <%= check_box_tag field_name, element.last, selected, :id => "ms#{@multiple_select_count}_#{attribute_name}_#{element.last}" %>
78
+ <%- else -%>
79
+ <%= check_box_tag field_name, element.id, selected, :id => "ms#{@multiple_select_count}_#{table_name}_#{attribute_name}_#{element.id}" %>
80
+ <%- end -%>
107
81
 
108
82
  <%- elsif kind == :radio -%>
109
- <%- if defined?(record) -%>
110
- <%- selected = (params[table_name] ? params[table_name]["#{table_name}[#{klass.generic_field_associations[attribute_name][:foreign_key]}]"] == element.id.to_s : record.send(klass.generic_field_associations[attribute_name][:foreign_key]) == element.id) -%>
111
- <%- else -%>
112
- <%- selected = params[table_name] && params[table_name] == element.id.to_s -%>
83
+ <%- if mode == :activerecord -%>
84
+ <%- if defined?(record) -%>
85
+ <%- selected = (params[table_name] ? params[table_name]["#{klass.generic_field_associations[attribute_name][:foreign_key]}"] == element.id.to_s : record.send(klass.generic_field_associations[attribute_name][:foreign_key]) == element.id) -%>
86
+ <%- else -%>
87
+ <%- selected = params[table_name] && params[table_name] == element.id.to_s -%>
88
+ <%- end -%>
89
+ <%- elsif mode == :collection -%>
90
+ <%- if element.is_a? Array -%>
91
+ <%- selected = params[field_name].to_s == element.last.to_s -%>
92
+ <%- else -%>
93
+ <%- selected = false -%>
94
+ <%- end -%>
113
95
  <%- end -%>
114
96
 
115
- <%= radio_button_tag field_name, element.id, selected, :id => "ms#{@mutliple_select_count}_#{table_name}_#{attribute_name}_#{element.id}" %>
97
+ <%- if mode == :collection and element.is_a? Array -%>
98
+ <%= radio_button_tag field_name, element.last, selected, :id => "ms#{@multiple_select_count}_#{attribute_name}_#{element.last}" %>
99
+ <%- else -%>
100
+ <%= radio_button_tag field_name, element.id, selected, :id => "ms#{@multiple_select_count}_#{table_name}_#{attribute_name}_#{element.id}" %>
101
+ <%- end -%>
116
102
  <%- end -%>
117
103
 
118
- <%= label_tag "ms#{@mutliple_select_count}_#{table_name}_#{attribute_name}_#{element.id}", element.link_default_description %>
104
+ <%- if mode == :collection and element.is_a? Array -%>
105
+ <%= label_tag "ms#{@multiple_select_count}_#{attribute_name}_#{element.last}", element.first %>
106
+ <%- else -%>
107
+ <%= label_tag "ms#{@multiple_select_count}_#{table_name}_#{attribute_name}_#{element.id}", element.link_default_description %>
108
+ <%- end -%>
119
109
  </li>
120
110
  <%- end -%>
121
111
  </ul>
122
112
  <%- end -%>
123
-
124
- <%- @mutliple_select_count += 1 -%>
@@ -47,10 +47,10 @@
47
47
 
48
48
  <%- elsif klass.generic_field_associations[attribute_name] -%>
49
49
  <%- if klass.generic_field_associations[attribute_name][:kind] == :belongs_to -%>
50
- <%= render :partial => "generic_controller/multiple_select", :locals => { :kind => :radio, :field => field, :record => record } %>
50
+ <%= multiple_select :kind => :radio, :field => field, :record => record %>
51
51
 
52
52
  <%- elsif klass.generic_field_associations[attribute_name][:kind] == :has_and_belongs_to_many or klass.generic_field_associations[attribute_name][:kind] == :has_many-%>
53
- <%= render :partial => "generic_controller/multiple_select", :locals => { :kind => :check, :field => field, :record => record } %>
53
+ <%= multiple_select :kind => :check, :field => field, :record => record %>
54
54
 
55
55
  <%- end -%>
56
56
 
@@ -1,10 +1,8 @@
1
- <%- if defined?(QUI::Toolbar) -%>
2
- <%- toolbar do |t| -%>
3
- <%= t.index %>
4
- <%= t.separator %>
5
- <%= t.edit %>
6
- <%= t.delete %>
7
- <%- end -%>
1
+ <%- toolbar do |t| -%>
2
+ <%= t.index %>
3
+ <%= t.separator %>
4
+ <%= t.edit %>
5
+ <%= t.delete %>
8
6
  <%- end -%>
9
7
 
10
8
  <%- klass = @controller.class.to_s.demodulize.gsub("Controller", "").singularize.constantize -%>
@@ -31,13 +29,17 @@
31
29
 
32
30
  <%- tab_contents = {} -%>
33
31
  <%- record.class.generic_field_associations.each do |k,v| -%>
34
- <%- if (v[:kind] == :has_many or v[:kind] == :has_and_belongs_to_many) and not v[:through] -%>
32
+ <%- if (v[:kind] == :has_many or v[:kind] == :has_and_belongs_to_many) and (v[:generic_create] or not v[:through]) -%>
35
33
  <%- tab_contents[k] = capture do -%>
34
+ <%- if v[:generic_create] -%>
35
+ <%= link_to t(:"toolbar.create"), url_for([ "new", section, v[:class_name].table_name.singularize ]) + "?#{v[:class_name].table_name.singularize}[#{table_name}_id]=#{record.id}" %>
36
+ <%- end -%>
37
+
36
38
  <%= render :partial => "generic_controller/index_table", :locals => { :records => record.send(k), :class_name => v[:class_name] } %>
37
39
  <%- end -%>
38
40
  <%- end -%>
39
41
  <%- end -%>
40
42
 
41
- <%= tabs :tabs => [ { :header => { :text => :"#{section_prefix}tabs.common.description" }, :body => { :text => description } } ] + record.class.generic_field_associations.collect{|k,v| { :header => { :text => t(:"#{section_prefix}tabs.#{table_name}.#{k}") + " (#{record.send(k).count})" }, :body => { :text => tab_contents[k] } } if (v[:kind] == :has_many or v[:kind] == :has_and_belongs_to_many) and v[:through] == false and record.send(k).count > 0 }.compact -%>
43
+ <%= tabs :tabs => [ { :header => { :text => :"#{section_prefix}tabs.common.description" }, :body => { :text => description } } ] + record.class.generic_field_associations.collect{|k,v| { :header => { :text => t(:"#{section_prefix}tabs.#{table_name}.#{k}") + " (#{record.send(k).count})" }, :body => { :text => tab_contents[k] } } if (v[:kind] == :has_many or v[:kind] == :has_and_belongs_to_many) and v[:through] == false and (record.send(k).count > 0 or v[:generic_create]) }.compact -%>
42
44
 
43
45
 
@@ -68,7 +68,7 @@ module QM
68
68
  end
69
69
 
70
70
  def new
71
- instance_variable_set singular_variable, model.new unless instance_variable_defined?(singular_variable)
71
+ instance_variable_set singular_variable, model.new(params[singular_variable(true)]) unless instance_variable_defined?(singular_variable)
72
72
 
73
73
  respond_to do |format|
74
74
  format.html
@@ -77,8 +77,7 @@ module QM
77
77
  end
78
78
 
79
79
  def create
80
- instance_variable_set(singular_variable, model.new) unless instance_variable_defined?(singular_variable)
81
- instance_variable_get(singular_variable).update_attributes params[singular_variable(true)]
80
+ instance_variable_set(singular_variable, model.new(params[singular_variable(true)])) unless instance_variable_defined?(singular_variable)
82
81
 
83
82
  respond_to do |format|
84
83
  if instance_variable_get(singular_variable).save
@@ -91,7 +90,7 @@ module QM
91
90
  format.xml { render :xml => instance_variable_get(singular_variable), :status => :created, :location => instance_variable_get(singular_variable) }
92
91
  end
93
92
  else
94
- format.html { render :template => "generic_controller/form" }
93
+ format.html { render :action => "new" }
95
94
  format.xml { render :xml => instance_variable_get(singular_variable), :status => :unprocessable_entity }
96
95
  end
97
96
  end
@@ -13,14 +13,18 @@ module QM
13
13
 
14
14
  def has_many(association_id, options = {}, &extension)
15
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 ), :readonly => options[:readonly] || false, :through => options[:through].present? }
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 ), :readonly => options[:readonly] || false, :through => options[:through].present?, :generic_create => options[:generic_create] || false }
17
+
18
+ options.delete :generic_create
17
19
 
18
20
  super association_id, options, &extension
19
21
  end
20
22
 
21
23
  def has_and_belongs_to_many(association_id, options = {}, &extension)
22
24
  @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 ), :readonly => options[:readonly] || false, :through => options[:through].present? }
25
+ @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 ), :readonly => options[:readonly] || false, :through => options[:through].present?, :generic_create => options[:generic_create] || false }
26
+
27
+ options.delete :generic_create
24
28
 
25
29
  super association_id, options, &extension
26
30
  end
@@ -3,8 +3,54 @@ module QM
3
3
  module ViewIncludes
4
4
  def multiple_select(options = {})
5
5
  options.symbolize_keys!
6
+
7
+ @multiple_select_count ||= 0
8
+ @multiple_select_count += 1
9
+
10
+ if options.has_key?(:field) and not options[:field].nil? and options.has_key?(:record) and options[:record].is_a? ActiveRecord::Base
11
+ if options[:field].is_a? Hash
12
+ attribute_name = options[:field].keys.first.to_sym
13
+ else
14
+ attribute_name = options[:field].to_sym
15
+ end
16
+
17
+ klass = options[:record].class
18
+ table_name = klass.to_s.tableize.singularize
19
+
20
+ if options[:kind] == :check
21
+ field_name = "#{table_name}[#{attribute_name.to_s.singularize}_ids][]"
22
+ allow_nil = false
23
+
24
+ elsif options[:kind] == :radio
25
+ field_name = "#{table_name}[#{klass.generic_field_associations[attribute_name][:foreign_key]}]"
26
+ allow_nil ||= options[:record].column_for_attribute(klass.generic_field_associations[attribute_name][:foreign_key]).null
27
+ end
28
+
29
+ collection = klass.generic_field_associations[attribute_name][:class_name].all
30
+ render :partial => "generic_controller/multiple_select", :locals => { :mode => :activerecord,
31
+ :attribute_name => attribute_name,
32
+ :field_name => field_name,
33
+ :allow_nil => allow_nil,
34
+ :kind => options[:kind],
35
+ :klass => klass,
36
+ :table_name => table_name,
37
+ :record => options[:record],
38
+ :collection => collection }
39
+
40
+
41
+ elsif options.has_key? :collection and options[:collection].is_a? Array and options.has_key? :field_name and not options[:field_name].nil?
42
+ render :partial => "generic_controller/multiple_select", :locals => { :mode => :collection,
43
+ :field_name => options[:field_name],
44
+ :attribute_name => options[:field_name],
45
+ :table_name => options[:field_name],
46
+ :allow_nil => false,
47
+ :kind => options[:kind],
48
+ :collection => options[:collection] }
49
+
50
+ else
51
+ raise ArgumentError, "You must pass some variables as multiple_select. Possibilities are: 'field' and 'record' for automatic associations, or 'collection' and 'field_name' just for displaying a set."
52
+ end
6
53
 
7
- render :partial => "generic_controller/multiple_select", :locals => options
8
54
  end
9
55
  end
10
56
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qm-acts-as-generic-controller
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 11
10
- version: 0.0.11
9
+ - 12
10
+ version: 0.0.12
11
11
  platform: ruby
12
12
  authors:
13
13
  - Marcin Lewandowski
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-29 00:00:00 +02:00
18
+ date: 2010-09-13 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -35,9 +35,41 @@ dependencies:
35
35
  type: :runtime
36
36
  version_requirements: *id001
37
37
  - !ruby/object:Gem::Dependency
38
- name: rfc822
38
+ name: qui-tabs
39
39
  prerelease: false
40
40
  requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 25
46
+ segments:
47
+ - 0
48
+ - 0
49
+ - 3
50
+ version: 0.0.3
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: qui-toolbar
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 23
62
+ segments:
63
+ - 0
64
+ - 0
65
+ - 4
66
+ version: 0.0.4
67
+ type: :runtime
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: rfc822
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
41
73
  none: false
42
74
  requirements:
43
75
  - - ">="
@@ -47,11 +79,11 @@ dependencies:
47
79
  - 0
48
80
  version: "0"
49
81
  type: :runtime
50
- version_requirements: *id002
82
+ version_requirements: *id004
51
83
  - !ruby/object:Gem::Dependency
52
84
  name: sort_alphabetical
53
85
  prerelease: false
54
- requirement: &id003 !ruby/object:Gem::Requirement
86
+ requirement: &id005 !ruby/object:Gem::Requirement
55
87
  none: false
56
88
  requirements:
57
89
  - - ">="
@@ -61,11 +93,11 @@ dependencies:
61
93
  - 0
62
94
  version: "0"
63
95
  type: :runtime
64
- version_requirements: *id003
96
+ version_requirements: *id005
65
97
  - !ruby/object:Gem::Dependency
66
98
  name: calendar_date_select
67
99
  prerelease: false
68
- requirement: &id004 !ruby/object:Gem::Requirement
100
+ requirement: &id006 !ruby/object:Gem::Requirement
69
101
  none: false
70
102
  requirements:
71
103
  - - ">="
@@ -75,7 +107,7 @@ dependencies:
75
107
  - 0
76
108
  version: "0"
77
109
  type: :runtime
78
- version_requirements: *id004
110
+ version_requirements: *id006
79
111
  description: "qM: generates generic controller actions and views"
80
112
  email: marcin@saepia.net
81
113
  executables: []