Module: ActsAsStaticRecord::SingletonMethods

Defined in:
lib/acts_as_static_record.rb

Instance Method Summary collapse

Instance Method Details

#[](lookup_name) ⇒ Object

Similar to acts_as_enumeration model returns the record where the acts_as_static_record :key option matches lookup If no key is specified, the primary_id column is used

class User < ActiveRecord::Base
  acts_as_static_record :key => :user_name
end

Then later we can reference the objects by the user_name

User[:blythe]
User['snowgiraffe']

The key used will be the underscore version of the name. Punctuation marks are removed. The following are equivalent:

User[:blythe_snowgiraffeawesome]
User['blythe-SNOWGIRaffe   AWESome']

user = User.first
User[user.user_name] == user


297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/acts_as_static_record.rb', line 297

def [](lookup_name)
  (static_record_cache[:lookup]||= begin

    static_record_cache[:primary_key].inject({}) do |lookup, (k,v)|

      lookup[v.static_record_lookup_key] = v
      lookup

    end
  end)[static_record_lookup_key(lookup_name)]

end

#calculate_with_static_record(operation, column_name, options = {}) ⇒ Object

Override calculate to compute data in memory if possible Only processes results if there is no scope and the no keys other than distinct



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/acts_as_static_record.rb', line 361

def calculate_with_static_record(operation, column_name, options={})#:nodoc:
  if scope(:find).nil? && !options.any?{ |k,v| k.to_s.downcase != 'distinct' }
    key = "#{operation}_#{column_name}_#{options.none?{|k,v| v.blank? }}"
    static_record_cache[:calc][key]||=
      case operation.to_s
      when 'count' then
          #count the cache if we want all or the unique primary key
          if ['all', '', '*', primary_key].include?(column_name.to_s)
            static_record_cache[:primary_key].length

          #otherwise compute the length of the output
          else
            static_records_for_calculation(column_name, options) {|records| records.length }
          end
      #compute the method directly on the result array
      when 'sum', 'max', 'min' then

          if columns_hash[column_name.to_s].try(:type) == :integer
            static_records_for_calculation(column_name, options) {|records| records.send(operation).to_i }
          end

      end

    return static_record_cache[:calc][key] if static_record_cache[:calc][key]

  end
  calculate_without_static_record(operation, column_name, options)
end

#clear_static_record_cacheObject

Clear (and reload) the record cache



402
403
404
# File 'lib/acts_as_static_record.rb', line 402

def clear_static_record_cache
  @static_record_cache = nil
end

#find_in_static_record_cache(finder, attributes) ⇒ Object

Search the cache for records with the specified attributes

  • finder - Same as with find specify :all, :last or :first :all</all> returns an array of active records, <tt>:last or :first returns a single instance

  • attributes - a hash map of fields (or methods) => values

    User.find_in_static_cache(:first, => ‘fun’, :user_name => ‘giraffe’)



332
333
334
335
336
337
338
339
340
341
# File 'lib/acts_as_static_record.rb', line 332

def find_in_static_record_cache(finder, attributes)#:nodoc:
  list = static_record_cache[:primary_key].values.inject([]) do |list, record|
    unless attributes.select{|k,v| record.send(k).to_s != v.to_s}.any?
      return record if finder == :first
      list << record
    end
    list
  end
  finder == :all ? list : list.last
end

#find_with_static_record(*args) ⇒ Object

Perform find by searching through the static record cache if only an id is specified



345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/acts_as_static_record.rb', line 345

def find_with_static_record(*args)#:nodoc:
  if scope(:find).nil? && args
    if args.first.is_a?(Fixnum) &&
        ((args.length == 1 ||
        (args[1].is_a?(Hash) && args[1].values.delete(nil).nil?)))
      return static_record_cache[:primary_key][args.first]
    elsif args.first == :all && args.length == 1
      return static_record_cache[:primary_key].values
    end
  end

  find_without_static_record(*args)
end

#static_record_cacheObject

The static record cache



407
408
409
# File 'lib/acts_as_static_record.rb', line 407

def static_record_cache
  @static_record_cache||= initialize_static_record_cache
end

#static_record_lookup_key(value) ⇒ Object

Parse the lookup key



311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/acts_as_static_record.rb', line 311

def static_record_lookup_key(value)#:nodoc:
  if value.is_a? self
    static_record_lookup_key(
      value.send(
        acts_as_static_record_options[:lookup_key] ||
        acts_as_static_record_options[:key] ||
        primary_key
      )
    )
  else
    value.to_s.gsub(' ', '_').gsub(/\W/, "").underscore
  end
end

#static_records_for_calculation(column_name, options = {}, &block) ⇒ Object

Return the array of results to calculate if they are available



391
392
393
394
395
396
397
398
399
# File 'lib/acts_as_static_record.rb', line 391

def static_records_for_calculation(column_name, options={}, &block)#:nodoc:
  if columns_hash.has_key?(column_name.to_s)
    records = static_record_cache[:primary_key].values.collect(&(column_name.to_sym)).compact
    results = (options[:distinct]||options['distinct']) ? records.uniq : records
    block ? yield(results) : results
  else
    nil
  end
end