Module: Sinatra::ConstantHandler

Extended by:
Hexacta
Defined in:
lib/sinatra/handlers/constants.rb

Constant Summary

Constants included from Hexacta

Hexacta::GEM_FILE_DIR

Instance Method Summary collapse

Methods included from Hexacta

copy_all_files, copy_dir_structure, copy_file, gem_path, setup_dir

Instance Method Details

#enable_constantsObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sinatra/handlers/constants.rb', line 6

def enable_constants
  p "Enabling constants..."
  ConstantHandler.setup_dir("/app/views/#{Hexacta::GEM_FILE_DIR}/constants")
  ConstantHandler.copy_all_files("/lib/sinatra/views/constants","/app/views/#{Hexacta::GEM_FILE_DIR}/constants")

  before '/constant*' do
    error(403) unless authenticated(User).admin?
  end

  get '/constants' do
    params[:format] = 'group' if params[:format].nil?
    query = Constant.where(:kind => Constant.descendants.collect { |constant_class| constant_class.to_s })
    slim :"#{Hexacta::GEM_FILE_DIR}/constants/#{params[:format]}", locals: { :constants => query.order(:kind,:value).all, :total => query.count, :filters => params, :query => query }
  end

  post '/constant' do
    attributes = params.select { |attribute| Constant.columns.include?(attribute.to_sym) }
    if params[:values].nil?
      constant = Constant.create(attributes)
    else
      for value in params[:values].split(',')
        attributes[:value] = value
        constant = Constant.find_or_create(attributes)
      end
    end
    redirect back
  end

  post '/constants/:id' do |id|
    constant = Constant.find(:id => id.to_i)
    constant.update_fields(params, Constant.columns, :missing=>:skip)
    constant.save #This is import to force before save
    redirect back
  end

  delete '/constants/:id' do |id|
    constant = Constant.find(:id => id.to_i)
    constant.destroy.to_hash.to_json
  end

end