0

我计划在 Comfortable Mexican Sofa 中使用片段来存储电子邮件模板。但是有没有办法将字段添加到片段中。我会用它来存储电子邮件的主题。最好在 cms 中也包含该主题,以便我们的编辑可以根据需要更改它。

默认情况下,片段似乎只有两个字段“标签”和“标识符”。当然还有“内容”。我还想在字段中添加一个“主题”字符串。

4

1 回答 1

1

简单的三步过程:

1:

rails g migration AddSubjectToComfyCmsSnippets subject:string

2:

rake db:migrate

3:

app/views/comfy/admin/cms/snippets/_form.html.haml使用以下内容创建:

- 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'

现在您可以像这样在您的应用程序中引用主题:

Subject: #{@snippet.subject}

让固定装置工作的猴子补丁:

config/initializers/cms_monkey_patch.rb使用以下内容创建:

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
于 2014-09-06T13:45:59.593 回答