module RailsConnector class ObjsController < WebserviceController before_filter :load_object, only: [:edit, :update, :destroy, :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 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 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 { |key| p[key] = convert_html_links(p[key]) } if widget_pool_params = p['_widget_pool'] convert_widget_pool_params(@obj, widget_pool_params) end end end end def convert_widget_pool_params(obj, widget_pool_params) widget_pool_params.each_pair do |widget_id, widget_params| widget = obj.widget_from_pool(widget_id) widget_params.each_pair do |param_name, param_value| if widget.type_of_attribute(param_name) == 'html' widget_params[param_name] = convert_html_links(param_value) end end end end def convert_html_links(html) ContentConversion.convert_html_links(html, request.host, request.port) 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)}" 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