Sha256: eaee3a48ed8b7acf5a1dc884811acde0a952dcb6bbe8621cede4dea2560564c8

Contents?: true

Size: 1.4 KB

Versions: 12

Compression:

Stored size: 1.4 KB

Contents

require 'sinatra/base'

module Coprl
  module Presenters
    module Demo
      class DragonDrop < Sinatra::Base
        include Coprl::Presenters::WebClient::Helpers::ExpandHash

        configure do
          enable :sessions
          set :session_secret, '0e53ba1da13aa8eb8cf3d361e67b70d79265edca'
        end

        post('/_dragon_drop_change_order_') do
          content_type :json
          source_id = JSON.parse(params[:source_id]) if params[:source_id]
          target_id = JSON.parse(params[:target_id]) if params[:target_id]
          dragons = fetch_dragons
          unless source_id == target_id
            source = dragons.select {|d| d.id == source_id}.first
            target = dragons.select {|d| d.id == target_id}.first

            if source && target
              starting_sort = target.sort
              source.sort = starting_sort
              dragons.each do |dragon|
                next if dragon.id == source.id || dragon.sort < starting_sort
                dragon.sort += 1
              end
            end
            save(dragons)
          end
        end

        def fetch_dragons
          JSON.parse(session[:dragons], object_class: OpenStruct)
        end

        def save(dragons)
          session[:dragons] = dragons.map {|d| expand_hash(d)}.to_json
        end

        # start the server if ruby file executed directly
        run! if app_file == $0
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
coprl-3.0.0.beta.12 lib/coprl/presenters/demo/dragon_drop.rb
coprl-3.0.0.beta.11 lib/coprl/presenters/demo/dragon_drop.rb
coprl-3.0.0.beta.10 lib/coprl/presenters/demo/dragon_drop.rb
coprl-3.0.0.beta.9 lib/coprl/presenters/demo/dragon_drop.rb
coprl-3.0.0.beta.8 lib/coprl/presenters/demo/dragon_drop.rb
coprl-3.0.0.beta.7 lib/coprl/presenters/demo/dragon_drop.rb
coprl-3.0.0.beta.6 lib/coprl/presenters/demo/dragon_drop.rb
coprl-3.0.0.beta.5 lib/coprl/presenters/demo/dragon_drop.rb
coprl-3.0.0.beta.4 lib/coprl/presenters/demo/dragon_drop.rb
coprl-3.0.0.beta.3 lib/coprl/presenters/demo/dragon_drop.rb
coprl-3.0.0.beta.2 lib/coprl/presenters/demo/dragon_drop.rb
coprl-3.0.0.beta.1 lib/coprl/presenters/demo/dragon_drop.rb