【问题标题】:How to add a field into a snippet in Comfortable Mexican Sofa如何在舒适的墨西哥沙发中将字段添加到片段中
【发布时间】:2014-09-05 12:35:14
【问题描述】:

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

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

【问题讨论】:

  • 为什么不使用“标签”来设置电子邮件的主题?这样您就不必修改 CMS。
  • 本来我以为换标签的时候也会改变标识符,但是好像是第一次保存sn-p之前的情况。我想我可以使用标签。谢谢!

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


【解决方案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

【讨论】:

  • 尝试这个时的一大缺点。使用rake comfortable_mexican_sofa:fixtures:export 将 cms 导出到固定装置时,不会从数据库中导出主题字段。是否有一个同样简单的步骤来使导出命令包含自定义数据库列?进口也一样?我们使用固定装置将网站从登台发布到生产。
  • @Mika。我添加了一个解决方案,我相当有信心,它会让固定装置工作。我没有测试它。如果您尝试它但它不起作用,请告诉我。我只是在this 代码中添加了两行
  • 顺便说一句,我觉得我上面的建议变得有点强硬了。就个人而言,我会尽可能避免此类修改。如果你实现了它并且它有效,你可能只想在更新 cms gem 时保持警惕。
  • 谢谢。这当然看起来是一种可行的方法。我目前的解决方案是为该主题设置一个单独的 sn-p。虽然它在不修改 CMS 代码的情况下工作,但它很快在 sn-p 部分变得有点混乱。在完成一些优先级更高的任务后,我需要回到这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多