module RailsConnector class ObjsController < WebserviceController before_filter :load_object, only: [:edit, :update, :destroy, :revert, :show_widget, :widget_class_selection, :create_widget, :edit_widget] def create created_obj = task_unaware_request(:post, "workspaces/#{Workspace.current.id}/objs", {obj: obj_params} ) render json: created_obj end def edit render json: {markup: render_to_string(@obj.edit_view_path, layout: false)} end def update changed_obj = task_unaware_request(:put, "workspaces/#{Workspace.current.id}/objs/#{params[:id]}", {obj: obj_params} ) render json: changed_obj end def destroy @obj.destroy render json: {} end def revert @obj.revert render json: {} end def copy render json: copy_obj(get_obj_attributes(params[:id]), params[:parent_path]) end def duplicate attributes = get_obj_attributes(params[:id]) render json: copy_obj(attributes, parent_path(attributes['_path'])) end def page_class_selection valid_page_classes = Obj.valid_page_classes_beneath(params[:parent_path]) || all_page_classes valid_page_classes.map!(&:to_s) page_class_names = valid_page_classes.map do |page_class_name| begin markup = render_to_string("#{page_class_name.underscore}/thumbnail") {name: page_class_name, markup: markup} rescue ActionView::MissingTemplate end end render json: page_class_names.compact end def show_widget @widget = @obj.widget_from_pool(params[:widget_id]) render json: {markup: render_to_string(layout: false)} end def widget_class_selection valid_widget_classes = @obj.valid_widget_classes_for(params[:field_name]) || all_widget_classes valid_widget_classes.map!(&:to_s) widgets_classes = valid_widget_classes.map do |widget_class_name| markup = WidgetRenderer.new(request).process('thumbnail', widget_class_name.underscore) {name: widget_class_name, markup: markup} end render json: widgets_classes end def create_widget widget_pool_id = BasicObj.generate_widget_pool_id task_unaware_request(:put, "workspaces/#{Workspace.current.id}/objs/#{params[:id]}", {obj: {_widget_pool: {widget_pool_id => {_obj_class: params[:obj_class]}}}}) Workspace.current = Workspace.find(Workspace.current.id) @obj.reload @widget = @obj.widget_from_pool(widget_pool_id) render json: {markup: render_to_string(layout: false)} end def edit_widget @widget = @obj.widget_from_pool(params[:widget_id]) markup = WidgetRenderer.new(request) .process(:edit, @widget, current_page, params[:field_name], @obj) render json: {markup: markup} end def search query = MultiJson.decode(params[:query]).with_indifferent_access search_builder = ObjSearchBuilder.new(query) enumerator = search_builder.build if params[:query_action] == 'size' result = { total: enumerator.size } else batch = enumerator.load_batch result = { total: enumerator.size, hits: batch } end render json: result rescue ObjSearchEnumerator::UnregisteredObjFormat => e render json: { error: e.message }, status: :not_found end private def load_object @obj = Obj.find(params[:id]) end def obj_params ObjParamsParser.new(request.host, request.port).parse(@obj, params[:obj]) end def copy_obj(attributes, target_path=nil) copied_attributes = attributes.except!('id', '_permalink') copied_attributes['_path'] = "#{target_path}/#{SecureRandom.hex(6)}" task_unaware_request(:post, "workspaces/#{Workspace.current.id}/objs", obj: copied_attributes) end def get_obj_attributes(id) task_unaware_request(:get, "workspaces/#{Workspace.current.id}/objs/#{id}") end def parent_path(path) path.split('/')[0..-2].join('/') end def current_page Obj.find(params[:current_page_id]) if params[:current_page_id].present? end helper_method :current_page def all_page_classes page_classes = task_unaware_request(:get, "workspaces/#{Workspace.current.id}/obj_classes")['results'] page_classes.map { |page_class| page_class['name'] }.sort end def all_widget_classes Dir[Rails.root + 'app/widgets/*'].map { |path| File.basename(path).camelize }.sort end delegate :task_unaware_request, to: CmsRestApi end end