Sha256: 0472f7d8ca4d724d492ffc295ffeca04144fd603ba6b559f2149576912774600
Contents?: true
Size: 1.74 KB
Versions: 23
Compression:
Stored size: 1.74 KB
Contents
module ActiveRecordExtension extend ActiveSupport::Concern # add your instance methods here def foo 'foo' end # add your static(class) methods here module ClassMethods def grab(which,what,options) pluck=Assist.make_string_list(what) unless options[:swap].nil? swaps=options[:swap] swaps.each_key { |key| index=what.index(key.to_sym) what[index]=swaps[key.to_sym] } end order=Assist.make_string_list(options[:sort]) unless options.nil? || options[:sort].nil? if which=='all' order.nil? ? Assist.named_array(self.all.pluck(pluck),what) : Assist.named_array(self.all.order(order).pluck(pluck),what) else order.nil? ? Assist.named_array(self.where(which).pluck(pluck),what) : Assist.named_array(self.where(which).order(order).pluck(pluck),what) end end end module Assist def self.make_string_list(alist) plucking='' alist.each {|entry| plucking.concat(',' ) unless plucking=='' plucking.concat(entry.to_s) } plucking end def self.named_array(data,what) data = Array.new(what.size,nil) if data.nil? || data.empty? if data.size==1 double=true data.push(data[0]) end pseudo= !data[0].is_a?(Array) && what.size>1 temp=Array.new{{}} pseudo ? size=1 : size=data.size (0...size).each {|i| temp[i]={} datax=data[i] (0...what.size).each { |j| pseudo ? temp[i].store(what[j], data[j]) : temp[i].store(what[j], datax[j]) } } temp.shift if double temp end end end # include the extension ActiveRecord::Base.send(:include, ActiveRecordExtension)
Version data entries
23 entries across 23 versions & 1 rubygems