Class: Kilt::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/kilt/utils.rb

Class Method Summary collapse

Class Method Details

.config_is_valid?Boolean



98
99
100
101
102
# File 'lib/kilt/utils.rb', line 98

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_configObject



26
27
28
29
30
31
# File 'lib/kilt/utils.rb', line 26

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

.databaseObject



18
19
20
21
22
23
24
# File 'lib/kilt/utils.rb', line 18

def self.database
  @database ||= if @db_type == :active_record
                  Kilt::DB::ActiveRecord.new
                else
                  Kilt::DB::RethinkDb.new current_db_config
                end
end

.deslugify(str) ⇒ Object

Break down a slug



92
93
94
95
96
# File 'lib/kilt/utils.rb', line 92

def self.deslugify(str)
  ret = str.to_s
  ret.gsub! /_/, " "   
  ret.capitalize
end

.download_location(type, value) ⇒ Object

Get a file download URL



53
54
55
56
57
58
59
# File 'lib/kilt/utils.rb', line 53

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_existsObject

Ensure we have local storage dirs



34
35
36
37
38
# File 'lib/kilt/utils.rb', line 34

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_existsObject

Ensure we have an s3 bucket



41
42
43
44
45
46
47
48
49
50
# File 'lib/kilt/utils.rb', line 41

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) ⇒ Boolean

Determine if the string passed in is singular or not



62
63
64
# File 'lib/kilt/utils.rb', line 62

def self.is_singular?(str)
  str.pluralize != str && str.singularize == str
end

.setup_dbObject



4
5
6
7
8
9
10
# 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) ⇒ Object

Create a slug



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/kilt/utils.rb', line 67

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

.tipsObject

Print a list of objects, functions, etc.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/kilt/utils.rb', line 105

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://&lt;your_app&gt;/admin'
  end
  lines.join("\n")
end

.use_db(db_type) ⇒ Object



12
13
14
15
16
# 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