Sha256: 514106d4b73f30017d41ed9db5ba09a20523ebb5dcdabbfeb5bb0e771cf5a12c

Contents?: true

Size: 1.64 KB

Versions: 29

Compression:

Stored size: 1.64 KB

Contents

# encoding: utf-8
module Sinatra
  module ConstantHandler
    extend Hexacta

    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

  end

  register ConstantHandler
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
sinatra-hexacta-1.7.20 lib/sinatra/handlers/constants.rb
sinatra-hexacta-1.7.19 lib/sinatra/handlers/constants.rb
sinatra-hexacta-1.7.18 lib/sinatra/handlers/constants.rb
sinatra-hexacta-1.7.17 lib/sinatra/handlers/constants.rb
sinatra-hexacta-1.7.16 lib/sinatra/handlers/constants.rb
sinatra-hexacta-1.7.15 lib/sinatra/handlers/constants.rb
sinatra-hexacta-1.7.14 lib/sinatra/handlers/constants.rb
sinatra-hexacta-1.7.13 lib/sinatra/handlers/constants.rb
sinatra-hexacta-1.7.12 lib/sinatra/handlers/constants.rb
sinatra-hexacta-1.7.11 lib/sinatra/handlers/constants.rb
sinatra-hexacta-1.7.10 lib/sinatra/handlers/constants.rb
sinatra-hexacta-1.7.9 lib/sinatra/handlers/constants.rb
sinatra-hexacta-1.7.8 lib/sinatra/handlers/constants.rb
sinatra-hexacta-1.7.7 lib/sinatra/handlers/constants.rb
sinatra-hexacta-1.7.6 lib/sinatra/handlers/constants.rb
sinatra-hexacta-1.7.5 lib/sinatra/handlers/constants.rb
sinatra-hexacta-1.7.4 lib/sinatra/handlers/constants.rb
sinatra-hexacta-1.7.3 lib/sinatra/handlers/constants.rb
sinatra-hexacta-1.7.2 lib/sinatra/handlers/constants.rb
sinatra-hexacta-1.7.0 lib/sinatra/handlers/constants.rb