radiant-parameterized_snippets-extension 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,47 @@
1
+ = Radiant 'Parameterized Snippets' Extension
2
+
3
+ Call snippets with parameters which can be reused in the snippet.
4
+
5
+
6
+ = Installation
7
+
8
+ In your Rails app directory:
9
+
10
+ git clone git://github.com/manuelmeurer/radiant-parameterized-snippets-extension.git vendor/extensions/parameterized_snippets
11
+
12
+ Or download the ZIP or TAR file from http://github.com/manuelmeurer/radiant-parameterized-snippets-extension/ and extract to vendor/extensions/parameterized_snippets
13
+
14
+
15
+ = Usage
16
+
17
+ In some page:
18
+
19
+ What's up with elephants?
20
+
21
+ <r:snippet name="animal_info" animal="elephant" />
22
+
23
+ And lions? What do lions do?
24
+
25
+ <r:snippet name="animal_info" animal="lion" />
26
+
27
+ In snippet 'animal_info':
28
+
29
+ <r:unless_var name="animal">
30
+ What animal do you want info about?
31
+ Please set the 'animal' parameter when calling this snippet!
32
+ </r:unless_var>
33
+ <r:if_var name="animal">
34
+ <r:if_var name="animal" matches=".*cat.*">
35
+ So you wanna know something about a cat?
36
+ Could be a 'house cat', just a 'cat' or a 'caterpillar'...
37
+ But it is a <r:var name="animal" />!
38
+ </r:if_var>
39
+
40
+ <r:unless_var name="animal" matches="ant|mouse|eagle">
41
+ Glad you didn't ask about ants, mice or eagles! I don't know JACK about those animals!
42
+ </r:if_var>
43
+
44
+ Enimal? Is there an enimal? If yes, tell me what it is,
45
+ if no, don't you dare returning an error message! <r:var name="enimal" missing="ignore" />
46
+ </r:if_var>
47
+
@@ -0,0 +1,120 @@
1
+ # I think this is the one that should be moved to the extension Rakefile template
2
+
3
+ # In rails 1.2, plugins aren't available in the path until they're loaded.
4
+ # Check to see if the rspec plugin is installed first and require
5
+ # it if it is. If not, use the gem version.
6
+
7
+ # Determine where the RSpec plugin is by loading the boot
8
+ unless defined? RADIANT_ROOT
9
+ ENV["RAILS_ENV"] = "test"
10
+ case
11
+ when ENV["RADIANT_ENV_FILE"]
12
+ require File.dirname(ENV["RADIANT_ENV_FILE"]) + "/boot"
13
+ when File.dirname(__FILE__) =~ %r{vendor/radiant/vendor/extensions}
14
+ require "#{File.expand_path(File.dirname(__FILE__) + "/../../../../../")}/config/boot"
15
+ else
16
+ require "#{File.expand_path(File.dirname(__FILE__) + "/../../../")}/config/boot"
17
+ end
18
+ end
19
+
20
+ require 'rake'
21
+ require 'rake/rdoctask'
22
+ require 'rake/testtask'
23
+
24
+ rspec_base = File.expand_path(RADIANT_ROOT + '/vendor/plugins/rspec/lib')
25
+ $LOAD_PATH.unshift(rspec_base) if File.exist?(rspec_base)
26
+ require 'spec/rake/spectask'
27
+ # require 'spec/translator'
28
+
29
+ # Cleanup the RADIANT_ROOT constant so specs will load the environment
30
+ Object.send(:remove_const, :RADIANT_ROOT)
31
+
32
+ extension_root = File.expand_path(File.dirname(__FILE__))
33
+
34
+ task :default => :spec
35
+ task :stats => "spec:statsetup"
36
+
37
+ desc "Run all specs in spec directory"
38
+ Spec::Rake::SpecTask.new(:spec) do |t|
39
+ t.spec_opts = ['--options', "\"#{extension_root}/spec/spec.opts\""]
40
+ t.spec_files = FileList['spec/**/*_spec.rb']
41
+ end
42
+
43
+ namespace :spec do
44
+ desc "Run all specs in spec directory with RCov"
45
+ Spec::Rake::SpecTask.new(:rcov) do |t|
46
+ t.spec_opts = ['--options', "\"#{extension_root}/spec/spec.opts\""]
47
+ t.spec_files = FileList['spec/**/*_spec.rb']
48
+ t.rcov = true
49
+ t.rcov_opts = ['--exclude', 'spec', '--rails']
50
+ end
51
+
52
+ desc "Print Specdoc for all specs"
53
+ Spec::Rake::SpecTask.new(:doc) do |t|
54
+ t.spec_opts = ["--format", "specdoc", "--dry-run"]
55
+ t.spec_files = FileList['spec/**/*_spec.rb']
56
+ end
57
+
58
+ [:models, :controllers, :views, :helpers].each do |sub|
59
+ desc "Run the specs under spec/#{sub}"
60
+ Spec::Rake::SpecTask.new(sub) do |t|
61
+ t.spec_opts = ['--options', "\"#{extension_root}/spec/spec.opts\""]
62
+ t.spec_files = FileList["spec/#{sub}/**/*_spec.rb"]
63
+ end
64
+ end
65
+
66
+ # Hopefully no one has written their extensions in pre-0.9 style
67
+ # desc "Translate specs from pre-0.9 to 0.9 style"
68
+ # task :translate do
69
+ # translator = ::Spec::Translator.new
70
+ # dir = RAILS_ROOT + '/spec'
71
+ # translator.translate(dir, dir)
72
+ # end
73
+
74
+ # Setup specs for stats
75
+ task :statsetup do
76
+ require 'code_statistics'
77
+ ::STATS_DIRECTORIES << %w(Model\ specs spec/models)
78
+ ::STATS_DIRECTORIES << %w(View\ specs spec/views)
79
+ ::STATS_DIRECTORIES << %w(Controller\ specs spec/controllers)
80
+ ::STATS_DIRECTORIES << %w(Helper\ specs spec/views)
81
+ ::CodeStatistics::TEST_TYPES << "Model specs"
82
+ ::CodeStatistics::TEST_TYPES << "View specs"
83
+ ::CodeStatistics::TEST_TYPES << "Controller specs"
84
+ ::CodeStatistics::TEST_TYPES << "Helper specs"
85
+ ::STATS_DIRECTORIES.delete_if {|a| a[0] =~ /test/}
86
+ end
87
+
88
+ namespace :db do
89
+ namespace :fixtures do
90
+ desc "Load fixtures (from spec/fixtures) into the current environment's database. Load specific fixtures using FIXTURES=x,y"
91
+ task :load => :environment do
92
+ require 'active_record/fixtures'
93
+ ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
94
+ (ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir.glob(File.join(RAILS_ROOT, 'spec', 'fixtures', '*.{yml,csv}'))).each do |fixture_file|
95
+ Fixtures.create_fixtures('spec/fixtures', File.basename(fixture_file, '.*'))
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
101
+
102
+ desc 'Generate documentation for the parameterized_snippets extension.'
103
+ Rake::RDocTask.new(:rdoc) do |rdoc|
104
+ rdoc.rdoc_dir = 'rdoc'
105
+ rdoc.title = 'ParameterizedSnippetsExtension'
106
+ rdoc.options << '--line-numbers' << '--inline-source'
107
+ rdoc.rdoc_files.include('README')
108
+ rdoc.rdoc_files.include('lib/**/*.rb')
109
+ end
110
+
111
+ # For extensions that are in transition
112
+ desc 'Test the parameterized_snippets extension.'
113
+ Rake::TestTask.new(:test) do |t|
114
+ t.libs << 'lib'
115
+ t.pattern = 'test/**/*_test.rb'
116
+ t.verbose = true
117
+ end
118
+
119
+ # Load any custom rakefiles for extension
120
+ Dir[File.dirname(__FILE__) + '/tasks/*.rake'].sort.each { |f| require f }
@@ -0,0 +1,94 @@
1
+ module ParameterizedSnippets
2
+ module PageExt
3
+
4
+ include Radiant::Taggable
5
+
6
+ desc %{
7
+ Renders the containing elements only if the snippet was called with a certain parameter.
8
+ If an additional "matches" parameter containing a regular expression is used, the containing
9
+ elements are only rendered if the parameter exists and matches the regular expression.
10
+
11
+ *Usage:*
12
+
13
+ <pre><code><r:if_var name="parameter_name" [matches="regex"]>...</r:if_var></code></pre>
14
+
15
+ *Example:*
16
+
17
+ Some page:
18
+
19
+ <pre><code><r:snippet name="animal_info" animal="elephant" /></code></pre>
20
+
21
+ animal_info snippet:
22
+
23
+ <pre><code><r:if_var name="animal" matches=".le(?:ph|f)ant">...</r:if_var></code></pre>
24
+ }
25
+ tag 'snippet:if_var' do |tag|
26
+ tag.expand if var_exists_and_matches(tag)
27
+ end
28
+
29
+ desc %{
30
+ The opposite of the @if_var@ tag.
31
+
32
+ *Usage:*
33
+
34
+ <pre><code><r:unless_var name="parameter_name" [matches="regex"]>...</r:unless_var></code></pre>
35
+ }
36
+ tag 'snippet:unless_var' do |tag|
37
+ tag.expand unless var_exists_and_matches(tag)
38
+ end
39
+
40
+ desc %{
41
+ Outputs the value of the parameter.
42
+ If the value cannot be found, an error message or, if you use the 'missing' parameter, nothing is shown.
43
+
44
+ *Usage:*
45
+
46
+ <pre><code><r:var name="parameter_name" [missing="ignore"] /></code></pre>
47
+
48
+ *Example:*
49
+
50
+ Some page:
51
+
52
+ <pre><code><r:snippet name="animal_info" animal="elephant" /></code></pre>
53
+
54
+ animal_info snippet:
55
+
56
+ <pre><code><r:var name="animal" /> # Outputs 'elephant'
57
+ <r:var name="enimal" missing="ignore" /> # Outputs nothing</code></pre>
58
+ }
59
+ tag 'snippet:var' do |tag|
60
+ ignore_missing = tag.attr['missing'] == 'ignore'
61
+ var = check_for_attr(tag, 'name')
62
+ tag_binding = get_snippet_tag_binding(tag)
63
+ if tag_binding && tag_binding.attr[var]
64
+ tag_binding.attr[var]
65
+ else
66
+ ignore_missing ? '' : "Could not find parameter '#{var}' in snippet '#{tag_binding.attributes['name']}'."
67
+ end
68
+ end
69
+
70
+ protected
71
+
72
+ def check_for_attr(tag, attr)
73
+ returning type = tag.attr[attr.to_s].to_s do
74
+ raise TagError, "Please define the '#{attr}' attribute for the tag '#{tag.name}'." unless type
75
+ end
76
+ end
77
+
78
+ def get_snippet_tag_binding(tag)
79
+ tag.context.instance_variable_get(:@tag_binding_stack).reverse.detect { |tag_binding| tag_binding.name == 'snippet' }
80
+ end
81
+
82
+ def var_exists_and_matches(tag)
83
+ var = check_for_attr(tag, 'name')
84
+ tag_binding = get_snippet_tag_binding(tag)
85
+ if tag.attr['matches']
86
+ tag_binding.attr[var] =~ /^#{tag.attr['matches']}$/
87
+ else
88
+ tag_binding.attr[var]
89
+ end
90
+ end
91
+
92
+ end
93
+ end
94
+
@@ -0,0 +1,8 @@
1
+ module RadiantParameterizedSnippetsExtension
2
+ AUTHORS = 'Manuel Meurer, Michael Starke (gemification)'
3
+ EMAIL = 'manuel.meurer@gmail.com'
4
+ DESCRIPTION = 'Allow sending parameters to snippets'
5
+ URL = 'http://github.com/mstarke/radiant-parameterized-snippets-extension'
6
+ VERSION = '1.0.1'
7
+ SUMMARY = 'Parameters in snippets'
8
+ end
@@ -0,0 +1,28 @@
1
+ namespace :radiant do
2
+ namespace :extensions do
3
+ namespace :parameterized_snippets do
4
+
5
+ desc "Runs the migration of the Parameterized Snippets extension"
6
+ task :migrate => :environment do
7
+ require 'radiant/extension_migrator'
8
+ if ENV["VERSION"]
9
+ ParameterizedSnippetsExtension.migrator.migrate(ENV["VERSION"].to_i)
10
+ else
11
+ ParameterizedSnippetsExtension.migrator.migrate
12
+ end
13
+ end
14
+
15
+ desc "Copies public assets of the Parameterized Snippets to the instance public/ directory."
16
+ task :update => :environment do
17
+ is_svn_or_dir = proc {|path| path =~ /\.svn/ || File.directory?(path) }
18
+ Dir[ParameterizedSnippetsExtension.root + "/public/**/*"].reject(&is_svn_or_dir).each do |file|
19
+ path = file.sub(ParameterizedSnippetsExtension.root, '')
20
+ directory = File.dirname(path)
21
+ puts "Copying #{path}..."
22
+ mkdir_p RAILS_ROOT + directory
23
+ cp file, RAILS_ROOT + path
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "radiant-parameterized_snippets-extension"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "radiant-parameterized_snippets-extension"
7
+ s.version = RadiantParameterizedSnippetsExtension::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = RadiantParameterizedSnippetsExtension::AUTHORS
10
+ s.email = RadiantParameterizedSnippetsExtension::EMAIL
11
+ s.homepage = RadiantParameterizedSnippetsExtension::URL
12
+ s.summary = RadiantParameterizedSnippetsExtension::SUMMARY
13
+ s.description = RadiantParameterizedSnippetsExtension::DESCRIPTION
14
+
15
+ ignores = if File.exist?('.gitignore')
16
+ File.read('.gitignore').split("\n").inject([]) {|a,p| a + Dir[p] }
17
+ else
18
+ []
19
+ end
20
+ s.files = Dir['**/*'] - ignores
21
+ s.test_files = Dir['test/**/*','spec/**/*','features/**/*'] - ignores
22
+ s.require_paths = ["lib"]
23
+ end
@@ -0,0 +1,11 @@
1
+ class ParameterizedSnippetsExtension < Radiant::Extension
2
+
3
+ def activate
4
+ Page.send :include, ParameterizedSnippets::PageExt
5
+ end
6
+
7
+ def deactivate
8
+ end
9
+
10
+ end
11
+
@@ -0,0 +1,6 @@
1
+ --colour
2
+ --format
3
+ progress
4
+ --loadby
5
+ mtime
6
+ --reverse
@@ -0,0 +1,37 @@
1
+ unless defined? RADIANT_ROOT
2
+ ENV["RAILS_ENV"] = "test"
3
+ case
4
+ when ENV["RADIANT_ENV_FILE"]
5
+ require ENV["RADIANT_ENV_FILE"]
6
+ when File.dirname(__FILE__) =~ %r{vendor/radiant/vendor/extensions}
7
+ require "#{File.expand_path(File.dirname(__FILE__) + "/../../../../../../")}/config/environment"
8
+ else
9
+ require "#{File.expand_path(File.dirname(__FILE__) + "/../../../../")}/config/environment"
10
+ end
11
+ end
12
+ require "#{RADIANT_ROOT}/spec/spec_helper"
13
+
14
+ if File.directory?(File.dirname(__FILE__) + "/scenarios")
15
+ Scenario.load_paths.unshift File.dirname(__FILE__) + "/scenarios"
16
+ end
17
+ if File.directory?(File.dirname(__FILE__) + "/matchers")
18
+ Dir[File.dirname(__FILE__) + "/matchers/*.rb"].each {|file| require file }
19
+ end
20
+
21
+ Spec::Runner.configure do |config|
22
+ # config.use_transactional_fixtures = true
23
+ # config.use_instantiated_fixtures = false
24
+ # config.fixture_path = RAILS_ROOT + '/spec/fixtures'
25
+
26
+ # You can declare fixtures for each behaviour like this:
27
+ # describe "...." do
28
+ # fixtures :table_a, :table_b
29
+ #
30
+ # Alternatively, if you prefer to declare them only once, you can
31
+ # do so here, like so ...
32
+ #
33
+ # config.global_fixtures = :table_a, :table_b
34
+ #
35
+ # If you declare global fixtures, be aware that they will be declared
36
+ # for all of your examples, even those that don't use them.
37
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: radiant-parameterized_snippets-extension
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 1
9
+ version: 1.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Manuel Meurer, Michael Starke (gemification)
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2012-10-04 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Allow sending parameters to snippets
22
+ email: manuel.meurer@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/parameterized_snippets/page_ext.rb
31
+ - lib/radiant-parameterized_snippets-extension.rb
32
+ - lib/tasks/parameterized_snippets_extension_tasks.rake
33
+ - parameterized_snippets_extension.gemspec
34
+ - parameterized_snippets_extension.rb
35
+ - Rakefile
36
+ - README
37
+ - spec/spec.opts
38
+ - spec/spec_helper.rb
39
+ has_rdoc: true
40
+ homepage: http://github.com/mstarke/radiant-parameterized-snippets-extension
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.6
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Parameters in snippets
69
+ test_files:
70
+ - spec/spec.opts
71
+ - spec/spec_helper.rb