qm-acts-as-generic-controller 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -9,7 +9,7 @@ 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.6"
12
+ gemspec.version = "0.0.7"
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
15
  gemspec.add_dependency "rfc822"
@@ -0,0 +1,87 @@
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
+ <%- klass = record.class -%>
8
+ <%- table_name = klass.to_s.tableize.singularize -%>
9
+
10
+ <%- content_tag(:div, :class => "multiple_select_select", :id => "multiple_select_select_for_#{attribute_name}") do -%>
11
+ <%- content_tag(:div, :class => "filter") do -%>
12
+ <%= text_field_tag "multiple_select_filter_for_#{attribute_name}", "" %>
13
+ <%- javascript_tag do -%>
14
+ new Form.Element.Observer($("multiple_select_filter_for_<%= attribute_name %>"), 0.3, function() {
15
+ $("multiple_select_filter_for_<%= attribute_name %>").up(1).select('label').each(function(el) {
16
+ if(el.innerHTML.toLocaleLowerCase().include(this.value.toLocaleLowerCase()))
17
+ el.up().show();
18
+ else
19
+ el.up().hide();
20
+ }, $("multiple_select_filter_for_<%= attribute_name %>")
21
+ );
22
+ });
23
+
24
+ Event.observe(window, 'load', function() {
25
+ $("multiple_select_filter_for_<%= attribute_name %>").value = "";
26
+ $("multiple_select_select_for_<%= attribute_name %>").select("input").each(function(el) {
27
+ if(el.checked)
28
+ $(el).up().addClassName('selected');
29
+ else
30
+ $(el).up().removeClassName('selected');
31
+ });
32
+ });
33
+ <%- end -%>
34
+
35
+ <%- if kind == :check -%>
36
+ <%- javascript_tag do -%>
37
+ Event.observe(window, 'load', function() {
38
+ $("multiple_select_select_for_<%= attribute_name %>").select("input").each(function(el) {
39
+ el.onchange = function() {
40
+ if(this.checked)
41
+ $(this).up().addClassName('selected');
42
+ else
43
+ $(this).up().removeClassName('selected');
44
+ }
45
+ });
46
+ });
47
+ <%- end -%>
48
+
49
+ <%- elsif kind == :radio -%>
50
+ <%- javascript_tag do -%>
51
+ Event.observe(window, 'load', function() {
52
+ $("multiple_select_select_for_<%= attribute_name %>").select("input").each(function(el) {
53
+ el.onchange = function() {
54
+ if(this.checked) {
55
+ $(this).up(1).select("li").each(function(el) { el.removeClassName('selected') });
56
+ $(this).up().addClassName('selected');
57
+ }
58
+ }
59
+ });
60
+ });
61
+ <%- end -%>
62
+
63
+ <%- end -%>
64
+ <%- end -%>
65
+
66
+ <ul>
67
+ <%- if kind == :radio and record.column_for_attribute(klass.generic_field_associations[attribute_name][:foreign_key]).null -%>
68
+ <li class="null">
69
+ <%= radio_button_tag "#{table_name}[#{klass.generic_field_associations[attribute_name][:foreign_key]}]", "", record.send(klass.generic_field_associations[attribute_name][:foreign_key]).nil?, :id => "#{table_name}_#{attribute_name}_null" %>
70
+ <%= label_tag "#{table_name}_#{attribute_name}_null", t(:"genericController.multipleSelectNull") %>
71
+ </li>
72
+ <%- end -%>
73
+
74
+ <%- klass.generic_field_associations[attribute_name][:class_name].all.each do |element| -%>
75
+ <li>
76
+ <%- if kind == :check -%>
77
+ <%= check_box_tag "#{table_name}[#{attribute_name.to_s.singularize}_ids][]", element.id, (params[table_name] ? 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) ), :id => "#{table_name}_#{attribute_name}_#{element.id}" %>
78
+
79
+ <%- elsif kind == :radio -%>
80
+ <%= radio_button_tag "#{table_name}[#{klass.generic_field_associations[attribute_name][:foreign_key]}]", element.id, (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), :id => "#{table_name}_#{attribute_name}_#{element.id}" %>
81
+ <%- end -%>
82
+ <%= label_tag "#{table_name}_#{attribute_name}_#{element.id}", element.link_default_description %>
83
+ </li>
84
+ <%- end -%>
85
+ </ul>
86
+ <%- end -%>
87
+
@@ -1,11 +1,13 @@
1
+ <%- record = instance_variable_get("@#{@controller.class.to_s.demodulize.gsub("Controller", "").tableize.singularize}") -%>
2
+ <%- klass = @controller.class.to_s.demodulize.gsub("Controller", "").singularize.constantize -%>
3
+ <%- table_name = "#{@controller.class.to_s.demodulize.gsub("Controller", "").tableize.singularize}" -%>
4
+
1
5
  <%- if defined?(section) -%>
2
- <%- form_for_param = [ section, instance_variable_get("@#{@controller.class.to_s.demodulize.gsub("Controller", "").tableize.singularize}") ] -%>
6
+ <%- form_for_param = [ section, record ] -%>
3
7
  <%- else -%>
4
- <%- form_for_param = instance_variable_get("@#{@controller.class.to_s.demodulize.gsub("Controller", "").tableize.singularize}") -%>
8
+ <%- form_for_param = record -%>
5
9
  <%- end -%>
6
10
 
7
- <%- klass = @controller.class.to_s.demodulize.gsub("Controller", "").singularize.constantize -%>
8
- <%- table_name = "#{@controller.class.to_s.demodulize.gsub("Controller", "").tableize.singularize}" -%>
9
11
  <%- form_for form_for_param do |f| -%>
10
12
  <%- unless klass.generic_form_fieldsets -%>
11
13
  <fieldset>
@@ -20,23 +22,31 @@
20
22
  <%- attribute_name = field.to_sym -%>
21
23
  <%- end -%>
22
24
 
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 } %>
25
+ <%- if not klass.generic_field_associations[attribute_name] or (klass.generic_field_associations[attribute_name] and not klass.generic_field_associations[attribute_name][:readonly]) -%>
26
+ <li>
27
+ <%= f.label attribute_name %>
28
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][klass.generic_field_associations[attribute_name][:foreign_key]].to_i), { :include_blank => f.object.column_for_attribute(klass.generic_field_associations[attribute_name][:foreign_key]).null }, { :name => "#{table_name}[#{klass.generic_field_associations[attribute_name][:foreign_key]}]" } %>
29
+ <%- if field.is_a?(Hash) and field.values.first == :enum -%>
30
+ <%= 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 } %>
31
+
32
+ <%- elsif klass.generic_field_associations[attribute_name] -%>
33
+ <%- if klass.generic_field_associations[attribute_name][:kind] == :belongs_to -%>
34
+ <%= render :partial => "generic_controller/multiple_select", :locals => { :kind => :radio, :field => field, :record => record } %>
35
+
36
+ <%- elsif klass.generic_field_associations[attribute_name][:kind] == :has_and_belongs_to_many or klass.generic_field_associations[attribute_name][:kind] == :has_many-%>
37
+ <%= render :partial => "generic_controller/multiple_select", :locals => { :kind => :check, :field => field, :record => record } %>
38
+
39
+ <%- end -%>
40
+
32
41
  <%- else -%>
33
-
42
+ <%- if record.column_for_attribute(attribute_name).type == :text -%>
43
+ <%= f.text_area attribute_name, :rows => 5, :cols => 60 %>
44
+ <%- else -%>
45
+ <%= f.text_field attribute_name %>
46
+ <%- end -%>
34
47
  <%- end -%>
35
-
36
- <%- else -%>
37
- <%= f.text_field attribute_name %>
38
- <%- end -%>
39
- </li>
48
+ </li>
49
+ <%- end -%>
40
50
  <%- end -%>
41
51
 
42
52
  </ol>
@@ -13,14 +13,14 @@ 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 ) }
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 }
17
17
 
18
18
  super association_id, options, &extension
19
19
  end
20
20
 
21
21
  def has_and_belongs_to_many(association_id, options = {}, &extension)
22
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 ) }
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 }
24
24
 
25
25
  super association_id, options, &extension
26
26
  end
@@ -28,7 +28,7 @@ module QM
28
28
  def belongs_to(association_id, options = {})
29
29
  @generic_field_associations ||= {}
30
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 }
31
+ @generic_field_associations[association_id.to_sym] = { :kind => :belongs_to, :class_name => klass, :foreign_key => options[:foreign_key] || "#{klass.to_s.tableize}_id" }
32
32
 
33
33
  super association_id, options
34
34
  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: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 6
10
- version: 0.0.6
9
+ - 7
10
+ version: 0.0.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Marcin Lewandowski
@@ -74,6 +74,7 @@ files:
74
74
  - MIT-LICENSE
75
75
  - Rakefile
76
76
  - app/views/generic_controller/_data.erb
77
+ - app/views/generic_controller/_multiple_select.erb
77
78
  - app/views/generic_controller/form.erb
78
79
  - app/views/generic_controller/index.erb
79
80
  - app/views/generic_controller/show.erb