Sha256: 81c30d1c60dec156a13c910e58870c50cbde2f392ca19cf0a41197c47a79554b

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

require_dependency "polyblock/application_controller"

module Polyblock
  class PolyblocksController < ApplicationController

    def update
      if params.key?(:pbs)
        puts params.inspect
        params[:pbs].each do |i, attrs|
          next if attrs[:content].nil?
          permitted_attrs = attrs.permit(:id, :name, :content, :_destroy)
          id = permitted_attrs.delete :id
          puts permitted_attrs.inspect
          name = permitted_attrs[:name]
          matching_block = if id.present?
                             Block.find_by :id => id.to_i
                           elsif name.present?
                             Block.find_by :name => name
                           end
          if matching_block.present?
            matching_block.update_attributes!(permitted_attrs) 
          else
            Block.create! permitted_attrs
          end
        end
        render :text => "OK!"
      elsif params.key?(:polyblocks)
        params[:polyblocks].each do |name, pb_attrs|
          pb = Block.fetch_or_create name
          pb.update_attributes! pb_attrs
        end
        flash[:success] = 'Your polyblocks have been updated!'
        redirect_to :back
      end
    end

    def convert_haml_to_html
      haml = params[:haml]

      # Remove the pesky helpers
      haml = haml.split("= succeed \"")
      lines = [haml.shift]
      haml.each do |chunk|
        arg = chunk.split("\"",2)[0]
        block = chunk.split("\n")[1..-1].map{|line| line[2..-1]}.join("\n")
        lines << "#{block}\narg"
      end
      haml = lines.join("")

      # Run it through the engine
      render :text => Haml::Engine.new(haml).render
    end

    def convert_html_to_haml
      # html = if params.has_key? :cbid then ContentBlock.find(params[:cbid]).content else params[:html] end
      # engine = Html2haml::HTML.new html, :erb => false, :xhtml => false
      engine = Html2haml::HTML.new params[:html], :erb => false, :xhtml => false
      render :text => engine.render
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
polyblock-0.9.7 app/controllers/polyblock/polyblocks_controller.rb