Module: QM::ActsAsGenericController::ViewIncludes

Defined in:
lib/qm-acts-as-generic-controller-view.rb

Instance Method Summary collapse

Instance Method Details

#generic_index_table(options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/qm-acts-as-generic-controller-view.rb', line 40

def generic_index_table(options = {})
  records    = options[:records]    || instance_variable_get("@#{@controller.class.to_s.demodulize.gsub("Controller", "").tableize}")
  class_name = options[:class_name] || @controller.class.to_s.demodulize.gsub("Controller", "").singularize.constantize

  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

  headers = privileged_fields.collect{ |x| x[:options][:index_header] || ".#{x[:name]}".to_sym }

  controller_name = "#{defined?(section) ? "#{section.to_s.camelize}::" : ""}#{class_name.to_s.pluralize}".tableize

  has_edit = ActionController::Routing::Routes.routes.collect{|x| x if x.matches_controller_and_action?(controller_name, "edit") }.compact.size > 0
  has_delete = ActionController::Routing::Routes.routes.collect{|x| x if x.matches_controller_and_action?(controller_name, "destroy") }.compact.size > 0

  if current_user.respond_to? "has_privileges?"
    has_edit = has_edit && current_user.has_privileges?(:class_name => class_name, :generic_action => :update_any)
    has_delete = has_edit && current_user.has_privileges?(:class_name => class_name, :generic_action => :delete_any)
  end

  class_name.generic_actions(:action => :index).size.times{ headers << :oneicon }

  headers << :oneicon if has_edit
  headers << :oneicon if has_delete
  
  
  index_table records, :headers => headers, :class_name => class_name do |r|
    row = ""
    
    privileged_fields.each do |field|
      row << (:td) do
        if field[:options].has_key? :renderer
          send(field[:options][:renderer], { :record => r, :field => field, :action => :index }).to_s
        else
          generic_renderer(:index, { :record => r, :field => field }).to_s
        end
      end
    end

    class_name.generic_actions(:action => :index).each do |action|
      row << (:td, send(action[:options][:renderer], { :action => :index, :record => r, :action => :index, :generic_action => action }), :class => "action #{action[:name]}")
    end

    if has_edit
      row << (:td, :class => "action edit") do
        link_to_edit(r)
      end
    end
    
    if has_delete
      row << (:td, :class => "action delete") do
        link_to_delete(r)
      end
    end
    
    row
  end
end

#generic_renderer(action, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/qm-acts-as-generic-controller-view.rb', line 4

def generic_renderer(action, options = {})

  data = options[:record].send options[:field][:name]
  
  result = ""
  
  if data.is_a?(ActiveRecord::Base)
    begin 
      result = link_to_object options[:record].send(options[:field][:name])

    rescue NoMethodError
      result = data.link_default_description
    
    end

  elsif data.is_a?(Array) and data.first.is_a?(ActiveRecord::Base)
    result = data.collect{ |x| link_to_object(x) }.sort_alphabetical.join(", ")

  elsif options[:record].class.is_enum? options[:field][:name]
    result = t("activerecord.enums.#{options[:record].class.table_name.singularize}.#{options[:field][:name]}.#{data}")
    
  elsif options[:record].class.columns_hash[options[:field][:name].to_s].type == :boolean
    result = data ? image_tag("tick.png", :alt => t(:"genericController.yesAsBooleanOption")) : ""
  
  elsif defined?(RFC822) and data.is_a?(String) and data =~ RFC822::EMAIL_REGEXP_WHOLE
    result = link_to data, "mailto:#{data}"
    
  else
    result = h data
  end
  
  result = link_to_object options[:record], result if options[:field][:options][:link_to] == :show
  
  result
end

#index_table_th(content, options = {}) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/qm-acts-as-generic-controller-view.rb', line 97

def index_table_th(content, options = {}) 
  if options[:sort_by]
    (:th, index_table_th_sort(content, options), options)
  else
    (:th, content, options)
  end
end

#index_table_th_sort(content, options = {}) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/qm-acts-as-generic-controller-view.rb', line 105

def index_table_th_sort(content, options = {})
  c = ""
  if params[:sort_by] == options[:sort_by]
    if params[:sort_order] == "A"
      c << image_tag("sort-asc.png", :alt => "^", :class => "sort-icon")
    else
      c << image_tag("sort-desc.png", :alt => "v", :class => "sort-icon")
    end
  end
  
  link_to(content + c, params.merge({ :sort_by => options[:sort_by], :sort_order => options[:sort_order]}))
end

#multiple_select(options = {}) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/qm-acts-as-generic-controller-view.rb', line 119

def multiple_select(options = {})
  options.symbolize_keys!
  options[:item] ||= {}

  @multiple_select_count ||= 0 
  @multiple_select_count += 1 

  if options.has_key?(:field) and not options[:field].nil? and options.has_key?(:record) and options[:record].is_a? ActiveRecord::Base
    attribute_name = options[:field][:name]

    klass = options[:record].class 
    table_name = klass.to_s.tableize.singularize 

    if options[:kind] == :check 
      field_name = "#{table_name}[#{attribute_name.to_s.singularize}_ids][]"
      allow_nil = false 

    elsif options[:kind] == :radio 
      field_name = "#{table_name}[#{klass.generic_field_associations[attribute_name][:foreign_key]}]"
      allow_nil ||= options[:record].column_for_attribute(klass.generic_field_associations[attribute_name][:foreign_key]).null 
    end
    
    if klass.generic_field_associations[attribute_name][:class_name].respond_to? :limit_for_user and defined?(current_user)
      collection = klass.generic_field_associations[attribute_name][:class_name].limit_for_user(current_user)
    else
      collection = klass.generic_field_associations[attribute_name][:class_name].all 
    end
    
    render :partial => "generic_controller/multiple_select", :locals => { :mode => :activerecord,
                                                                          :attribute_name => attribute_name, 
                                                                          :field_name => field_name, 
                                                                          :allow_nil => allow_nil, 
                                                                          :kind => options[:kind], 
                                                                          :klass => klass, 
                                                                          :table_name => table_name,
                                                                          :record => options[:record], 
                                                                          :collection => collection,
                                                                          :item => options[:item] }


  elsif options.has_key? :collection and options[:collection].is_a? Array and options.has_key? :field_name and not options[:field_name].nil?
    render :partial => "generic_controller/multiple_select", :locals => { :mode => :collection,
                                                                          :field_name => options[:field_name],
                                                                          :attribute_name => options[:field_name],
                                                                          :table_name => options[:field_name],
                                                                          :allow_nil => false, 
                                                                          :kind => options[:kind], 
                                                                          :collection => options[:collection],
                                                                          :item => options[:item] }

  else 
    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." 
  end 
        
end