module RailsConnector class ObjsController < WebserviceController before_filter :load_object, only: [:show, :edit, :update, :destroy, :widget_class_selection, :create_widget, :edit_widget] def show render json: {markup: render_to_string(layout: false)} end def create created_obj = CmsRestApi.task_unaware_request(:post, "revisions/#{Workspace.current.revision_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 = CmsRestApi.task_unaware_request(: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 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 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 = CmsRestApi.task_unaware_request(:post, "revisions/#{Workspace.current.revision_id}/objs", { obj: { _path: "/_widgets/#{@obj.id}/#{SecureRandom.hex}", _obj_class: params[:obj_class], }, } ) @widget = Obj.find(widget['id']) render json: {markup: render_to_string(layout: false)} end def edit_widget markup = WidgetRenderer.new(request). process(:edit, @obj, current_page, params[:field_name], @obj.widget_container) render json: {markup: markup} 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 def copy_obj(copied_attributes, target_path=nil) copied_attributes.delete('id') copied_attributes.delete('_last_changed') copied_attributes['_path'] = "#{target_path}/#{SecureRandom.hex(6)}" CmsRestApi.task_unaware_request( :post, "revisions/#{Workspace.current.revision_id}/objs", obj: copied_attributes) end def get_obj_attributes(id) CmsRestApi.task_unaware_request( :get, "revisions/#{Workspace.current.revision_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 = CmsRestApi.task_unaware_request(:get, "revisions/#{Workspace.current.revision_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 end end