module RailsConnector class ObjsController < WebserviceController before_filter :load_object, only: [:update, :destroy, :create_widget] def create created_obj = CmsRestApi.post( "revisions/#{Workspace.current.revision_id}/objs", {obj: obj_params} ) render json: created_obj end def update changed_obj = CmsRestApi.put( "revisions/#{Workspace.current.revision_id}/objs/#{params[:id]}", {obj: obj_params} ) render json: changed_obj end def destroy @obj.destroy render json: {} end def page_class_selection thumbnails = {} obj_classes = CmsRestApi.get("revisions/#{Workspace.current.revision_id}/obj_classes") obj_classes['results'].each do |obj_class| begin name = obj_class['name'] thumbnails[name] = render_to_string("#{name.underscore}/thumbnail") rescue ActionView::MissingTemplate end end render json: thumbnails end def widget_class_selection widgets = {} Dir[Rails.root + 'app/widgets/*'].each do |widget_path| widget_dir = File.basename(widget_path) widget_class_name = widget_dir.camelize widgets[widget_class_name] = WidgetRenderer.new(request).process('thumbnail', widget_dir) end render json: widgets end def create_widget widget = CmsRestApi.post("revisions/#{Workspace.current.revision_id}/objs", {obj: { _path: "/_widgets/#{@obj.id}/#{SecureRandom.hex}", _obj_class: params[:obj_class], }}) @widget = Obj.find(widget['id']) @current_page = Obj.find(params[:current_page_id]) if params[:current_page_id].present? render json: {markup: render_to_string(layout: false)} end private def load_object @obj = Obj.find(params[:id]) end def obj_params params[:obj].tap do |p| raise "Required parameter 'obj' is missing." unless p.present? raise "Parameter 'obj' is not a hash." unless p.is_a?(Hash) if @obj convert_html_keys = p.keys.select do |key| @obj.type_of_attribute(key.to_s) == 'html' end convert_html_keys.each do |key| p[key] = ContentConversion.convert_html_links(p[key], request.host, request.port) end end end end end end