Module: QM::ActsAsGenericController::ViewIncludes
- Defined in:
- lib/qm-acts-as-generic-controller-view.rb
Instance Method Summary collapse
- #generic_index_table(options = {}) ⇒ Object
- #generic_renderer(action, options = {}) ⇒ Object
- #index_table_th(content, options = {}) ⇒ Object
- #index_table_th_sort(content, options = {}) ⇒ Object
- #multiple_select(options = {}) ⇒ Object
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( = {}) records = [:records] || instance_variable_get("@#{@controller.class.to_s.demodulize.gsub("Controller", "").tableize}") class_name = [: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 << content_tag(: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 << content_tag(:td, send(action[:options][:renderer], { :action => :index, :record => r, :action => :index, :generic_action => action }), :class => "action #{action[:name]}") end if has_edit row << content_tag(:td, :class => "action edit") do link_to_edit(r) end end if has_delete row << content_tag(: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, = {}) data = [:record].send [:field][:name] result = "" if data.is_a?(ActiveRecord::Base) begin result = link_to_object [:record].send([: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 [:record].class.is_enum? [:field][:name] result = t("activerecord.enums.#{[:record].class.table_name.singularize}.#{[:field][:name]}.#{data}") elsif [:record].class.columns_hash[[: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 [:record], result if [: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, = {}) if [:sort_by] content_tag(:th, index_table_th_sort(content, ), ) else content_tag(:th, content, ) 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, = {}) c = "" if params[:sort_by] == [: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 => [:sort_by], :sort_order => [: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( = {}) .symbolize_keys! [:item] ||= {} @multiple_select_count ||= 0 @multiple_select_count += 1 if .has_key?(:field) and not [:field].nil? and .has_key?(:record) and [:record].is_a? ActiveRecord::Base attribute_name = [:field][:name] klass = [:record].class table_name = klass.to_s.tableize.singularize if [:kind] == :check field_name = "#{table_name}[#{attribute_name.to_s.singularize}_ids][]" allow_nil = false elsif [:kind] == :radio field_name = "#{table_name}[#{klass.generic_field_associations[attribute_name][:foreign_key]}]" allow_nil ||= [: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 => [:kind], :klass => klass, :table_name => table_name, :record => [:record], :collection => collection, :item => [:item] } elsif .has_key? :collection and [:collection].is_a? Array and .has_key? :field_name and not [:field_name].nil? render :partial => "generic_controller/multiple_select", :locals => { :mode => :collection, :field_name => [:field_name], :attribute_name => [:field_name], :table_name => [:field_name], :allow_nil => false, :kind => [:kind], :collection => [:collection], :item => [: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 |