qm-acts-as-generic-controller 0.1.21 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -10,10 +10,10 @@ begin
10
10
  gemspec.email = "marcin@saepia.net"
11
11
  gemspec.homepage = "http://q.saepia.net"
12
12
  gemspec.authors = ["Marcin Lewandowski"]
13
- gemspec.version = "0.1.21"
13
+ gemspec.version = "0.2.0"
14
14
  gemspec.files = Rake::FileList.new [ "MIT-LICENSE", "Rakefile", "lib/*", "app/views/generic_controller/*" ]
15
15
  gemspec.add_dependency "qui-common-helpers", ">= 0.0.8"
16
- gemspec.add_dependency "qui-index-table", ">= 0.0.8"
16
+ gemspec.add_dependency "qui-index-table", ">= 0.0.9"
17
17
  gemspec.add_dependency "qui-tabs", ">= 0.0.3"
18
18
  gemspec.add_dependency "qui-toolbar", ">= 0.0.5"
19
19
  gemspec.add_dependency "rfc822"
@@ -27,4 +27,4 @@
27
27
  <%- end -%>
28
28
  <%- end -%>
29
29
  <%- end -%>
30
- <%= render :partial => "generic_controller/index_table", :locals => { :records => records, :class_name => class_name } %>
30
+ <%= generic_index_table :records => records, :class_name => class_name %>
@@ -81,7 +81,7 @@
81
81
  <%= link_to t(:"toolbar.create"), polymorphic_path([ "new", section, v[:class_name].table_name.singularize ], { association_attribute => record.id, :association_attribute => association_attribute }), :class => "create" %>
82
82
  <%- end -%>
83
83
 
84
- <%= render :partial => "generic_controller/index_table", :locals => { :records => record.send(k).limit_with_security_scheme(:user => current_user), :class_name => v[:class_name] } %>
84
+ <%= generic_index_table :records => record.send(k).limit_with_security_scheme(:user => current_user), :class_name => v[:class_name] %>
85
85
  <%- tab_headers[k] = t(:"#{section_prefix}tabs.#{table_name}.#{k}") + " (#{record.send(k).limit_with_security_scheme(:user => current_user).size})" %>
86
86
  <%- end -%>
87
87
 
@@ -36,7 +36,7 @@ module QM
36
36
  module InstanceMethods
37
37
  def index
38
38
  unless instance_variable_defined?(plural_variable)
39
- instance_variable_set(plural_variable, model.limit_with_security_scheme(:user => current_user))
39
+ instance_variable_set(plural_variable, model.limit_with_security_scheme(:user => current_user, :paginate => true, :paginate_page => params[:page] || 1, :paginate_per_page => params[:per_page] || 100, :sort_by => params[:sort_by], :sort_order => params[:sort_order]))
40
40
  end
41
41
 
42
42
  respond_to do |format|
@@ -8,37 +8,81 @@ module QM
8
8
  module ClassMethods
9
9
  def limit_with_security_scheme(options)
10
10
  # options[:user] - current user that performs the query
11
+ # options[:paginate] - set to true if you want to enable pagination, not applicable for the ExternalSystem request
12
+ # options[:paginate_page] - set to integer
13
+ # options[:sort_by] - field to sort by
14
+ # options[:sort_order] - "A" or "D"
11
15
 
12
16
  raise ArgumentError, "You must pass options[:user] to limit_with_security_scheme" unless options.has_key? :user
13
17
 
14
18
  return all if options[:user].is_a? ExternalSystem
15
-
19
+
16
20
  # Return empty set if user has no privileges at all (nor index_any and index_created)
17
21
  unless options[:user].has_privileges?(:class_name => self, :generic_action => :index_any) or options[:user].has_privileges?(:class_name => self, :generic_action => :index_created)
18
- # puts ":("
19
- # puts options[:user].inspect
20
- # puts self
21
- # puts options[:user].has_privileges?(:class_name => self, :generic_action => :index_any)
22
- # puts options[:user].has_privileges?(:class_name => self, :generic_action => :index_created)
23
- # puts options[:user].has_privileges?(:class_name => self, :generic_action => :index_any) or options[:user].has_privileges?(:class_name => self, :generic_action => :index_created)
24
22
  return []
25
23
  end
26
24
 
27
25
  creator_column = "creator_#{options[:user].class.to_s.tableize.singularize}_id"
28
26
  should_limit_index_for_created = columns_hash.has_key?(creator_column) && options[:user].respond_to?(:has_privileges?) && !options[:user].has_privileges?(:class_name => self, :generic_action => :index_any) && options[:user].has_privileges?(:class_name => self, :generic_action => :index_created)
29
27
 
28
+ universal_find_params = {}
29
+
30
+ if options.has_key? :sort_by and not options[:sort_by].nil?
31
+ # Sorting
32
+ if options[:sort_order] == "A"
33
+ sort_direction = "ASC"
34
+ sort_opposite_direction = "DESC"
35
+ else
36
+ sort_direction = "DESC"
37
+ sort_opposite_direction = "ASC"
38
+ end
39
+
40
+ # FIXME what if this is not index?
41
+ generic_field = self.generic_fields[:index].detect{ |f| f[:name] == options[:sort_by].to_sym }
42
+ if generic_field and generic_field[:options].has_key? :sort_sql
43
+ universal_find_params[:order] = generic_field[:options][:sort_sql].gsub("%{DIRECTION}", sort_direction).gsub("%{OPPOSITE_DIRECTION}", sort_opposite_direction)
44
+
45
+ elsif self.columns_hash.has_key? options[:sort_by].to_s
46
+ universal_find_params[:order] = "#{options[:sort_by]} #{sort_direction}"
47
+ end
48
+ end
49
+
30
50
 
31
51
  if respond_to? :limit_for_user
32
52
  if should_limit_index_for_created
33
- limit_for_user(options[:user]).find(:all, :conditions => { creator_column => options[:user].id })
53
+ if defined? WillPaginate
54
+ logger.info "#{self}.limit_with_security_scheme: limit_for_user & limit_index_for_created & will_paginate(page = #{options[:paginate_page]}, per_page = #{options[:paginate_per_page]})"
55
+ limit_for_user(options[:user]).find(:all, universal_find_params).find(:all, :conditions => { creator_column => options[:user].id }).paginate(:page => options[:paginate_page], :per_page => options[:paginate_per_page])
56
+ else
57
+ logger.info "#{self}.limit_with_security_scheme: limit_for_user & limit_index_for_created"
58
+ limit_for_user(options[:user]).find(:all, universal_find_params).find(:all, :conditions => { creator_column => options[:user].id })
59
+ end
34
60
  else
35
- limit_for_user(options[:user])
61
+ if defined? WillPaginate
62
+ logger.info "#{self}.limit_with_security_scheme: limit_for_user & will_paginate(page = #{options[:paginate_page]}, per_page = #{options[:paginate_per_page]})"
63
+ limit_for_user(options[:user]).find(:all, universal_find_params).paginate(:page => options[:paginate_page], :per_page => options[:paginate_per_page])
64
+ else
65
+ logger.info "#{self}.limit_with_security_scheme: limit_for_user"
66
+ limit_for_user(options[:user]).find(:all, universal_find_params)
67
+ end
36
68
  end
37
69
  else
38
70
  if should_limit_index_for_created
39
- find(:all, :conditions => { creator_column => options[:user].id })
71
+ if defined? WillPaginate
72
+ logger.info "#{self}.limit_with_security_scheme: limit_index_for_created & will_paginate(page = #{options[:paginate_page]}, per_page = #{options[:paginate_per_page]})"
73
+ find(:all, :conditions => { creator_column => options[:user].id }).find(:all, universal_find_params).paginate(:page => options[:paginate_page], :per_page => options[:paginate_per_page])
74
+ else
75
+ logger.info "#{self}.limit_with_security_scheme: limit_index_for_created"
76
+ find(:all, :conditions => { creator_column => options[:user].id }).find(:all, universal_find_params)
77
+ end
40
78
  else
41
- all
79
+ if defined? WillPaginate
80
+ logger.info "#{self}.limit_with_security_scheme: will_paginate(page = #{options[:paginate_page]}, per_page = #{options[:paginate_per_page]})"
81
+ find(:all, universal_find_params).paginate(:page => options[:paginate_page], :per_page => options[:paginate_per_page])
82
+ else
83
+ logger.info "#{self}.limit_with_security_scheme: (all)"
84
+ find(:all, universal_find_params)
85
+ end
42
86
  end
43
87
  end
44
88
  end
@@ -36,6 +36,85 @@ module QM
36
36
 
37
37
  result
38
38
  end
39
+
40
+ def generic_index_table(options = {})
41
+ records = options[:records] || instance_variable_get("@#{@controller.class.to_s.demodulize.gsub("Controller", "").tableize}")
42
+ class_name = options[:class_name] || @controller.class.to_s.demodulize.gsub("Controller", "").singularize.constantize
43
+
44
+ privileged_fields = class_name.generic_fields(:action => :index).collect{|field| field if not current_user.respond_to? "has_privileges?" or (class_name.generic_field_associations.has_key?(field[:name]) and current_user.has_privileges?(:class_name => class_name.generic_field_associations[field[:name]][:class_name])) or (current_user.has_privileges?(:class_name => class_name, :attribute => field[:name], :mode => :read)) }.compact
45
+
46
+ headers = privileged_fields.collect{ |x| x[:options][:index_header] || ".#{x[:name]}".to_sym }
47
+
48
+ controller_name = "#{defined?(section) ? "#{section.to_s.camelize}::" : ""}#{class_name.to_s.pluralize}".tableize
49
+
50
+ has_edit = ActionController::Routing::Routes.routes.collect{|x| x if x.matches_controller_and_action?(controller_name, "edit") }.compact.size > 0
51
+ has_delete = ActionController::Routing::Routes.routes.collect{|x| x if x.matches_controller_and_action?(controller_name, "destroy") }.compact.size > 0
52
+
53
+ if current_user.respond_to? "has_privileges?"
54
+ has_edit = has_edit && current_user.has_privileges?(:class_name => class_name, :generic_action => :update_any)
55
+ has_delete = has_edit && current_user.has_privileges?(:class_name => class_name, :generic_action => :delete_any)
56
+ end
57
+
58
+ class_name.generic_actions(:action => :index).size.times{ headers << :oneicon }
59
+
60
+ headers << :oneicon if has_edit
61
+ headers << :oneicon if has_delete
62
+
63
+
64
+ index_table records, :headers => headers, :class_name => class_name do |r|
65
+ row = ""
66
+
67
+ privileged_fields.each do |field|
68
+ row << content_tag(:td) do
69
+ if field[:options].has_key? :renderer
70
+ send(field[:options][:renderer], { :record => r, :field => field, :action => :index }).to_s
71
+ else
72
+ generic_renderer(:index, { :record => r, :field => field }).to_s
73
+ end
74
+ end
75
+ end
76
+
77
+ class_name.generic_actions(:action => :index).each do |action|
78
+ row << content_tag(:td, send(action[:options][:renderer], { :action => :index, :record => r, :action => :index, :generic_action => action }), :class => "action #{action[:name]}")
79
+ end
80
+
81
+ if has_edit
82
+ row << content_tag(:td, :class => "action edit") do
83
+ link_to_edit(r)
84
+ end
85
+ end
86
+
87
+ if has_delete
88
+ row << content_tag(:td, :class => "action delete") do
89
+ link_to_delete(r)
90
+ end
91
+ end
92
+
93
+ row
94
+ end
95
+ end
96
+
97
+ def index_table_th(content, options = {})
98
+ if options[:sort_by]
99
+ content_tag(:th, index_table_th_sort(content, options), options)
100
+ else
101
+ content_tag(:th, content, options)
102
+ end
103
+ end
104
+
105
+ def index_table_th_sort(content, options = {})
106
+ c = ""
107
+ if params[:sort_by] == options[:sort_by]
108
+ if params[:sort_order] == "A"
109
+ c << image_tag("sort-asc.png", :alt => "^", :class => "sort-icon")
110
+ else
111
+ c << image_tag("sort-desc.png", :alt => "v", :class => "sort-icon")
112
+ end
113
+ end
114
+
115
+ link_to(content + c, params.merge({ :sort_by => options[:sort_by], :sort_order => options[:sort_order]}))
116
+ end
117
+
39
118
 
40
119
  def multiple_select(options = {})
41
120
  options.symbolize_keys!
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: 49
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 21
10
- version: 0.1.21
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
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: 2011-01-26 00:00:00 +01:00
18
+ date: 2011-02-07 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -42,12 +42,12 @@ dependencies:
42
42
  requirements:
43
43
  - - ">="
44
44
  - !ruby/object:Gem::Version
45
- hash: 15
45
+ hash: 13
46
46
  segments:
47
47
  - 0
48
48
  - 0
49
- - 8
50
- version: 0.0.8
49
+ - 9
50
+ version: 0.0.9
51
51
  type: :runtime
52
52
  version_requirements: *id002
53
53
  - !ruby/object:Gem::Dependency
@@ -150,8 +150,8 @@ homepage: http://q.saepia.net
150
150
  licenses: []
151
151
 
152
152
  post_install_message:
153
- rdoc_options: []
154
-
153
+ rdoc_options:
154
+ - --charset=UTF-8
155
155
  require_paths:
156
156
  - lib
157
157
  required_ruby_version: !ruby/object:Gem::Requirement