From 1c56139b524e11dbb65d0df1fd5dd6028b0ac603 Mon Sep 17 00:00:00 2001 From: Todd D Nestor Date: Thu, 1 Jun 2017 11:52:50 -0700 Subject: [PATCH] Template from configuration or file (#48) * Allow templates from configuration and templates to both be used * Updated readme --- README.md | 11 +++++------ lib/shortcode/configuration.rb | 22 +++++++++++++--------- lib/shortcode/tag.rb | 30 ++++++++++++++++++++---------- spec/spec_helper.rb | 3 ++- spec/tag_spec.rb | 18 ++++++++++++++++++ 5 files changed, 58 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 64897f7..11fcc1f 100644 --- a/README.md +++ b/README.md @@ -139,10 +139,7 @@ shortcode.setup do |config| end ``` -If the `templates` config option is set all templates will be loaded from this hash, if a shortcode is encountered without a matching key in the `templates` config option -an exception will be raised. - -Note: it's NOT possible to load templates from a config option AND from the file system, you must either load all templates from the file system or define all templates in a config option. +Note: Templates can be loaded from either the file system or the configuration templates. If `check_config_templates_first` is set to true (the default value) on the configuration then it will check configuration templates first, and file system templates if it doesn't find one. If `check_config_templates_first` is set to false on the configuration it will check for a file system template first, and then configuration templates if it doesn't find one. If it doesn't find a template in either spot then it will raise an error. ### Custom Helpers @@ -257,10 +254,12 @@ shortcode.setup do |config| # location of the template files, default is "app/views/shortcode_templates" config.template_path = "support/templates/erb" - # a hash of templates passed as strings, if this is set it overrides the - # above template_path option. The default is nil + # a hash of templates passed as strings. config.templates = { gallery: 'template code' } + # a boolean option to set whether configuration templates are checked first or file system templates + config.check_config_templates_first = true + # an array of helper modules to make available within templates config.helpers = [CustomerHelper] diff --git a/lib/shortcode/configuration.rb b/lib/shortcode/configuration.rb index cd55c87..fcb2f9b 100644 --- a/lib/shortcode/configuration.rb +++ b/lib/shortcode/configuration.rb @@ -8,6 +8,9 @@ class Shortcode::Configuration # Allows templates to be set from strings rather than read from the filesystem attr_accessor :templates + # Allows setting whether templates on the configuration are checked first, or templates in the file system + attr_accessor :check_config_templates_first + # Assigns helper modules to be included in templates attr_accessor :helpers @@ -33,15 +36,16 @@ def self_closing_tags=(self_closing_tags) attr_accessor :use_attribute_quotes def initialize - @template_parser = :erb - @template_path = "app/views/shortcode_templates" - @templates = nil - @helpers = [] - @block_tags = [] - @self_closing_tags = [] - @attribute_quote_type = '"' - @use_attribute_quotes = true - @presenters = {} + @template_parser = :erb + @template_path = "app/views/shortcode_templates" + @templates = {} + @check_config_templates_first = true + @helpers = [] + @block_tags = [] + @self_closing_tags = [] + @attribute_quote_type = '"' + @use_attribute_quotes = true + @presenters = {} end def register_presenter(presenter) diff --git a/lib/shortcode/tag.rb b/lib/shortcode/tag.rb index c01e141..817985b 100644 --- a/lib/shortcode/tag.rb +++ b/lib/shortcode/tag.rb @@ -7,11 +7,10 @@ def initialize(name, configuration, attributes=[], content='', additional_attrib end def markup - return markup_from_config unless configuration.templates.nil? - template_files.each do |path| - return File.read(path) if File.file? path - end - raise Shortcode::TemplateNotFound, "Searched in:", template_files + template = first_priority_template + template = second_priority_template if template.nil? + return template unless template.nil? + raise Shortcode::TemplateNotFound, "No template found for #{@name} in configuration or files" end def render @@ -35,12 +34,23 @@ def render_template end end - def markup_from_config - if configuration.templates.has_key? @name.to_sym - configuration.templates[@name.to_sym] - else - raise Shortcode::TemplateNotFound, "configuration.templates does not contain the key #{@name.to_sym}" + def first_priority_template + configuration.check_config_templates_first ? markup_from_config : markup_from_file + end + + def second_priority_template + configuration.check_config_templates_first ? markup_from_file : markup_from_config + end + + def markup_from_file + template_files.each do |path| + return File.read(path) if File.file? path end + nil + end + + def markup_from_config + configuration.templates[@name.to_sym] end def template_files diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1f843a8..fda8ed7 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -22,7 +22,8 @@ Shortcode.setup do |config| config.template_parser = :erb config.template_path = File.join File.dirname(__FILE__), "support/templates/erb" - config.templates = nil + config.templates = {} + config.check_config_templates_first = true config.block_tags = [:quote, :collapsible_list, :item, :timeline_person, :rails_helper, :custom_helper] config.self_closing_tags = [:timeline_event, :timeline_info] config.attribute_quote_type = '"' diff --git a/spec/tag_spec.rb b/spec/tag_spec.rb index ed37317..19d24db 100644 --- a/spec/tag_spec.rb +++ b/spec/tag_spec.rb @@ -65,4 +65,22 @@ end + context "when templates exist both in configuration and file system" do + let(:tag) { Shortcode::Tag.new('quote', configuration ) } + + before(:each) do + configuration.templates[:quote] = '

<%= @content %>

' + end + + it 'uses the configuration template when check_config_templates_first is true' do + expect(tag.markup).to eq('

<%= @content %>

') + end + + it 'uses the file system template when check_config_templates_first is false' do + configuration.check_config_templates_first = false + + expect(tag.markup).to eq(File.open('spec/support/templates/erb/quote.html.erb').read) + end + end + end