SpontaneousCMS/spontaneous

View on GitHub
lib/spontaneous/rack/back/base.rb

Summary

Maintainability
A
45 mins
Test Coverage
module Spontaneous::Rack
  module Back
    class Base < ServerBase
      helpers  Helpers

      set :views, Proc.new { Spontaneous.application_dir + '/views' }
      set :show_exceptions, false

      def content_model
        site.model
      end

      def update_fields(content, field_data)
        return unless field_data
        Spontaneous::Field.update_asynchronously(site, content, field_data, user)
        json(content.reload)
      end

      def content_for_request(lock = false)
        content_model.db.transaction do
          dataset = lock ? content_model.for_update : content_model
          content = dataset.get(params[:id])
          halt 404 if content.nil?
          content.current_editor = user
          if box_id = content_model.schema.uids[params[:box_id]]
            box = content.boxes.detect { |b| b.schema_id == box_id }
            yield(content, box)
          else
            yield(content)
          end
        end
      end
    end
  end
end