class Kilt::Utils
Public Class Methods
config_is_valid?()
click to toggle source
# File lib/kilt/utils.rb, line 94 def self.config_is_valid? Kilt.config.empty? == false && current_db_config #!(Kilt.config.empty? || !Kilt.config.db) #!Kilt.config.empty? && Kilt.config.db end
current_db_config()
click to toggle source
# File lib/kilt/utils.rb, line 22 def self.current_db_config current_environment = (ENV['RAILS_ENV'].to_s == '' ? 'development' : ENV['RAILS_ENV']).to_sym Kilt.config[current_environment][:db] rescue Kilt.config[:db] end
database()
click to toggle source
# File lib/kilt/utils.rb, line 18 def self.database Kilt::DB::ActiveRecord.new end
deslugify(str)
click to toggle source
Break down a slug
# File lib/kilt/utils.rb, line 88 def self.deslugify(str) ret = str.to_s ret.gsub! /_/, " " ret.capitalize end
download_location(type, value)
click to toggle source
Get a file download URL
# File lib/kilt/utils.rb, line 49 def self.download_location(type, value) if Kilt.config.storage.strategy == 'local' "/uploads/#{type.to_s}/#{value.to_s}" elsif Kilt.config.storage.strategy == 's3' "http://#{Kilt.config.s3.bucket}.s3.amazonaws.com/#{type.to_s}/#{value.to_s}" end end
ensure_local_storage_dir_exists()
click to toggle source
Ensure we have local storage dirs
# File lib/kilt/utils.rb, line 30 def self.ensure_local_storage_dir_exists Dir.mkdir(Rails.root.join('public', 'uploads')) unless File.exists?(Rails.root.join('public', 'uploads')) Dir.mkdir(Rails.root.join('public', 'uploads', 'file')) unless File.exists?(Rails.root.join('public', 'uploads', 'file')) Dir.mkdir(Rails.root.join('public', 'uploads', 'image')) unless File.exists?(Rails.root.join('public', 'uploads', 'image')) end
ensure_s3_bucket_exists()
click to toggle source
Ensure we have an s3 bucket
# File lib/kilt/utils.rb, line 37 def self.ensure_s3_bucket_exists s3 = AWS::S3.new( :access_key_id => Kilt.config.s3.key, :secret_access_key => Kilt.config.s3.secret) puts s3 bucket = s3.buckets[Kilt.config.s3.bucket] if !bucket.exists? bucket = s3.buckets.create(Kilt.config.s3.bucket, :acl => :public_read) end end
is_singular?(str)
click to toggle source
Determine if the string passed in is singular or not
# File lib/kilt/utils.rb, line 58 def self.is_singular?(str) str.pluralize != str && str.singularize == str end
setup_db()
click to toggle source
# File lib/kilt/utils.rb, line 4 def self.setup_db return unless current_db_config if db_type = current_db_config[:type] use_db(db_type.to_sym) end database.setup! end
slugify(str)
click to toggle source
Create a slug
# File lib/kilt/utils.rb, line 63 def self.slugify(str) #strip the string ret = str.strip #blow away apostrophes ret.gsub! /['`]/,"" # @ --> at, and & --> and ret.gsub! /\s*@\s*/, " at " ret.gsub! /\s*&\s*/, " and " #replace all non alphanumeric, underscore or periods with dash ret.gsub! /\s*[^A-Za-z0-9]\s*/, '-' #convert double dash to single ret.gsub! /-+/, "-" #strip off leading/trailing dash ret.gsub! /\A[-\.]+|[-\.]+\z/, "" #return a downcase string ret.downcase end
tips()
click to toggle source
Print a list of objects, functions, etc.
# File lib/kilt/utils.rb, line 101 def self.tips lines = [] if config_is_valid? lines << '' # Store the first type so we can use it down below first_type = nil Kilt.types.each do |type| if !first_type first_type = type end lines << "#{type.capitalize}: " lines << " Kilt.#{type} -> Type definition" lines << " Kilt.#{type}.fields -> List of fields" lines << " Kilt.#{type.pluralize} -> Array of all #{type.capitalize} objects in natural order" lines << " Kilt.#{type.pluralize}.order -> Array of all #{type.capitalize} objects ordered by 'name' field" lines << " Kilt.#{type.pluralize}.order('age') -> Array of all #{type.capitalize} objects ordered by 'age' field" lines << " Kilt.#{type.pluralize}.order('age').group('section') -> Array of all #{type.capitalize} objects ordered by 'age' field, then grouped by the 'section' field" end lines << '' lines << 'Get object:' lines << ' Kilt.get(\'some-slug\')' lines << '' lines << 'Loop through objects:' lines << " Kilt.#{first_type.pluralize}.each do |#{first_type}|" lines << " puts #{first_type}['name']" lines << ' end' lines << '' lines << 'Loop through objects ordered by field \'age\':' lines << " Kilt.#{first_type.pluralize}.order('age').each do |#{first_type}|" lines << " puts #{first_type}['name']" lines << ' end' lines << '' lines << 'Loop through objects ordered by field \'name\' (the default), then grouped by field \'section\':' lines << " Kilt.#{first_type.pluralize}.order.group(\'section\').each do |section, #{first_type.downcase.pluralize}|" lines << ' puts section' lines << " #{first_type.downcase.pluralize}.each do |#{first_type.downcase}|" lines << " puts #{first_type.downcase}['name']" lines << " end" lines << ' end' else lines << 'The Kilt gem has been installed, but you haven\'t configured it yet.' lines << 'Start configuring Kilt by running the following command:' lines << '' lines << ' rails g kilt:backend' lines << '' lines << 'Then open config/kilt/config.yml and config/kilt/creds.yml, add your database information, define your data model, start Rails, and visit http://<your_app>/admin' end lines.join("\n") end
use_db(db_type)
click to toggle source
# File lib/kilt/utils.rb, line 12 def self.use_db db_type return if @db_type == db_type @db_type = db_type @database = nil end