Class: AdminAssistant::Index::RecordFinder
- Inherits:
-
Object
- Object
- AdminAssistant::Index::RecordFinder
- Defined in:
- lib/admin_assistant/index.rb
Instance Method Summary collapse
- #add_base_condition_sqls ⇒ Object
- #belongs_to_sort_column ⇒ Object
- #cache_total_entries(total_entries) ⇒ Object
- #caching_total_entries? ⇒ Boolean
- #conditions_from_settings ⇒ Object
- #find_include ⇒ Object
-
#initialize(index) ⇒ RecordFinder
constructor
A new instance of RecordFinder.
- #optimized_total_entries ⇒ Object
- #order_sql ⇒ Object
- #run ⇒ Object
- #search ⇒ Object
- #settings ⇒ Object
- #sort_column ⇒ Object
- #total_entries_cache_key ⇒ Object
Constructor Details
permalink #initialize(index) ⇒ RecordFinder
Returns a new instance of RecordFinder.
77 78 79 |
# File 'lib/admin_assistant/index.rb', line 77 def initialize(index) @index = index end |
Instance Method Details
permalink #add_base_condition_sqls ⇒ Object
[View source]
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/admin_assistant/index.rb', line 81 def add_base_condition_sqls if @index.controller_methods[:conditions_for_index] sql = @index.controller_methods[:conditions_for_index].call @ar_query.condition_sqls << sql if sql elsif conditions_from_settings if conditions_from_settings.respond_to?(:call) conditions_sql = conditions_from_settings.call @index.url_params else conditions_sql = conditions_from_settings end @ar_query.condition_sqls << conditions_sql if conditions_sql end end |
permalink #belongs_to_sort_column ⇒ Object
[View source]
95 96 97 |
# File 'lib/admin_assistant/index.rb', line 95 def belongs_to_sort_column @index.belongs_to_columns.detect { |c| c.name.to_s == @index.sort } end |
permalink #cache_total_entries(total_entries) ⇒ Object
[View source]
99 100 101 102 103 104 |
# File 'lib/admin_assistant/index.rb', line 99 def cache_total_entries(total_entries) Rails.cache.write( total_entries_cache_key, total_entries, :expires_in => settings.cache_total_entries ) end |
permalink #caching_total_entries? ⇒ Boolean
106 107 108 |
# File 'lib/admin_assistant/index.rb', line 106 def caching_total_entries? search.params.empty? && settings.cache_total_entries end |
permalink #conditions_from_settings ⇒ Object
[View source]
110 111 112 |
# File 'lib/admin_assistant/index.rb', line 110 def conditions_from_settings settings.conditions end |
permalink #find_include ⇒ Object
[View source]
114 115 116 117 118 119 120 |
# File 'lib/admin_assistant/index.rb', line 114 def find_include fi = settings.include || [] if by_assoc = belongs_to_sort_column fi << by_assoc.name end fi end |
permalink #optimized_total_entries ⇒ Object
[View source]
122 123 124 125 126 127 128 |
# File 'lib/admin_assistant/index.rb', line 122 def optimized_total_entries if settings.total_entries settings.total_entries.call elsif caching_total_entries? Rails.cache.read total_entries_cache_key end end |
permalink #order_sql ⇒ Object
[View source]
130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/admin_assistant/index.rb', line 130 def order_sql if (sc = sort_column) first_part = if (by_assoc = belongs_to_sort_column) by_assoc.order_sql_field else sc.name end "#{first_part} #{@index.sort_order}" else settings.sort_by end end |
permalink #run ⇒ Object
[View source]
143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/admin_assistant/index.rb', line 143 def run @ar_query = ARQuery.new( :order => order_sql, :include => find_include, :per_page => settings.per_page, :page => @index.url_params[:page] ) add_base_condition_sqls search.add_to_query @ar_query @ar_query.total_entries = optimized_total_entries records = @index.model_class.paginate(@ar_query.to_hash) if caching_total_entries? && @ar_query.to_hash[:total_entries].nil? cache_total_entries records.total_entries end records end |
permalink #search ⇒ Object
[View source]
158 159 160 |
# File 'lib/admin_assistant/index.rb', line 158 def search @index.search end |
permalink #settings ⇒ Object
[View source]
162 163 164 |
# File 'lib/admin_assistant/index.rb', line 162 def settings @index.settings end |
permalink #sort_column ⇒ Object
[View source]
166 167 168 169 170 171 172 173 174 |
# File 'lib/admin_assistant/index.rb', line 166 def sort_column if @index.sort @index.columns.detect { |c| c.name.to_s == @index.sort } || belongs_to_sort_column elsif settings.sort_by.is_a?(Symbol) @index.columns.detect { |c| c.name == settings.sort_by.to_s } end end |
permalink #total_entries_cache_key ⇒ Object
[View source]
176 177 178 179 180 181 182 183 |
# File 'lib/admin_assistant/index.rb', line 176 def total_entries_cache_key key = "AdminAssistant::#{@index.admin_assistant.controller_class.name}_count" if conditions = @ar_query.to_hash[:conditions] key << conditions.gsub(/\W/, '_') end key end |