如何将字段添加到舒适墨西哥沙发的片段中

时间:2014-09-05 12:35:15

标签: ruby-on-rails comfortable-mexican-sofa

我计划在Comfortable Mexican Sofa中使用片段来存储电子邮件模板。但是有没有办法将一个字段添加到一个片段中。我会用它来存储电子邮件的主题。在cms中加入主题也是很好的,这样我们的编辑就可以在他们感觉到的时候改变它。

默认情况下,代码段似乎只有两个字段“Label”和“Identifier”。当然还有'内容'。我想在字段中添加一个'主题'字符串。

1 个答案:

答案 0 :(得分: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