Search code examples
ruby-on-railscomfortable-mexican-sofa

How to add a field into a snippet in Comfortable Mexican Sofa


I plan to use snippets in Comfortable Mexican Sofa to store email templates. But is there a way to add a field into a snippet. I would use it to store the subject of the email. Would be good to have also the subject in the cms so that our editors can change it if they feel like it.

By default snippets seem to have only two fields 'Label' and 'Identifier'. And of course the 'Content'. I would like to add also a 'Subject' String into the fields.


Solution

  • Simple Three Step Process:

    1:

    rails g migration AddSubjectToComfyCmsSnippets subject:string
    

    2:

    rake db:migrate
    

    3:

    create app/views/comfy/admin/cms/snippets/_form.html.haml with the following content:

    - content_for :right_column do
      = render 'comfy/admin/cms/files/index'
    = render :partial => 'comfy/admin/cms/partials/snippet_form_before', :object => form
    = form.text_field :label, :data => {:slugify => @snippet.new_record?}
    = form.text_field :identifier, :data => {:slug => true}
    = form.text_field :subject
    = form.text_area :content, :data => {'cms-rich-text' => true}
    = render :partial => 'comfy/admin/cms/categories/form', :object => form
    = render :partial => 'comfy/admin/cms/partials/snippet_form_after', :object => form
    = form.form_group :class => 'form-actions' do
      = form.submit t(@snippet.new_record?? '.create' : '.update'), :class => 'btn btn-primary'
      = link_to t('.cancel'), comfy_admin_cms_site_snippets_path, :class => 'btn btn-link'
    

    Now you can reference the subject in your app like so:

    Subject: #{@snippet.subject}
    

    Monkey patch to get fixtures working:

    create config/initializers/cms_monkey_patch.rb with the following contents:

    ComfortableMexicanSofa::Fixture::Snippet::Importer.class_eval do
        def import!
          Dir["#{self.path}*/"].each do |path|
            identifier = path.split('/').last
            snippet = self.site.snippets.find_or_initialize_by(:identifier => identifier)
    
            # setting attributes
            categories = []
            if File.exists?(attrs_path = File.join(path, 'attributes.yml'))
              if fresh_fixture?(snippet, attrs_path)
                attrs = get_attributes(attrs_path)
    
                snippet.label = attrs['label']
                snippet.subject = attrs['subject']
                categories    = attrs['categories']
              end
            end
    
            # setting content
            %w(html haml).each do |extension|
              if File.exists?(content_path = File.join(path, "content.#{extension}"))
                if fresh_fixture?(snippet, content_path)
                  snippet.content = extension == "html" ? 
                    ::File.open(content_path).read : 
                    Haml::Engine.new(::File.open(content_path).read).render.rstrip
                end
              end
            end
    
            # saving
            if snippet.changed? || self.force_import
              if snippet.save
                save_categorizations!(snippet, categories)
                ComfortableMexicanSofa.logger.info("[FIXTURES] Imported Snippet \t #{snippet.identifier}")
              else
                ComfortableMexicanSofa.logger.warn("[FIXTURES] Failed to import Snippet \n#{snippet.errors.inspect}")
              end
            end
    
            self.fixture_ids << snippet.id
          end
    
          # cleaning up
          self.site.snippets.where('id NOT IN (?)', fixture_ids).each{ |s| s.destroy }
        end
      end
    end
    
    ComfortableMexicanSofa::Fixture::Snippet::Exporter.class_eval do
        def export!
          prepare_folder!(self.path)
    
          self.site.snippets.each do |snippet|
            snippet_path = File.join(self.path, snippet.identifier)
            FileUtils.mkdir_p(snippet_path)
    
            # writing attributes
            open(File.join(snippet_path, 'attributes.yml'), 'w') do |f|
              f.write({
                'label'       => snippet.label,
                'subject'       => snippet.subject,
                'categories'  => snippet.categories.map{|c| c.label}
              }.to_yaml)
            end
    
            # writing content
            open(File.join(snippet_path, 'content.html'), 'w') do |f|
              f.write(snippet.content)
            end
    
            ComfortableMexicanSofa.logger.info("[FIXTURES] Exported Snippet \t #{snippet.identifier}")
          end
        end
    end