Sha256: 173019b0d3712d0e7d1e892326c6e4bdddb168a9f4d16fd1618fae7115781bd3

Contents?: true

Size: 1.61 KB

Versions: 4

Compression:

Stored size: 1.61 KB

Contents

module Sunrise
  module Utils
    autoload :Transliteration, 'sunrise/utils/transliteration'
    autoload :Mysql, 'sunrise/utils/mysql'
    autoload :EvalHelpers, 'sunrise/utils/eval_helpers'
    autoload :CsvDocument, 'sunrise/utils/csv_document'
    autoload :SearchWrapper, 'sunrise/utils/search_wrapper'
    
    IMAGE_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/jpg', 'image/pjpeg', 'image/tiff', 'image/x-png']
    
    def self.get_model(model_name, *args)
      klass = lookup(["Sunrise", model_name.to_s.classify].join, Sunrise::AbstractModel)
      klass ? klass.new(*args) : nil
    end
  
    # Given a string +model_name+, finds the corresponding model class
    def self.lookup(model_name, klass = nil)
      klass ||= ActiveRecord::Base if Object.const_defined?("ActiveRecord")
      
      model = model_name.constantize rescue nil
      if model && model.is_a?(Class) && superclasses(model).include?(klass) && !model.abstract_class?
        model
      else
        nil
      end
    rescue LoadError
      Sunrise::Engine.logger.error "Error while loading '#{model_name}': #{$!}"
      nil
    end
    
    def self.superclasses(klass)
      superclasses = []
      while klass
        superclasses << klass.superclass if klass && klass.superclass
        klass = klass.superclass
      end
      superclasses
    end
    
    # Convert sort string to hash
    # "create_at_desc" to { :column => "created_at", :mode => "desc" }
    #
    def self.sort_to_hash(value)
      items = value.split('_')
      mode = items.pop
      column = items.join('_')
      
      {:column => column, :mode => mode}
    end
    
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sunrise-cms-0.4.2 lib/sunrise/utils.rb
sunrise-cms-0.4.1 lib/sunrise/utils.rb
sunrise-cms-0.4.0 lib/sunrise/utils.rb
sunrise-cms-0.3.3 lib/sunrise/utils.rb