radiant-group_children-extension 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # Group Children
2
+
3
+ This is an extension for [Radiant][1], put together by [jomz][2]
4
+
5
+ Sometimes you want to iterate over a list of children, and insert something after every n-th child.
6
+ Or, for css reasons you want to group children per 3.
7
+ Until now we have sometimes used javascript to alter markup, which is very ucky..
8
+
9
+ Here is the solution;
10
+
11
+ <r:children:grouped per="3">
12
+ <div>
13
+ <r:children:each> ... </r:children:each>
14
+ </div>
15
+ </r:children:grouped>
16
+
17
+ The children:grouped tag takes all the same arguments like children:each, except for pagination.
18
+ Furthermore, inside children:grouped, you can use r:if\_first and r:if\_last to check if you are in the first or last group.
19
+ The children:each within children:grouped does NOT take arguments; those have to be passed to the children:grouped tag itself.
20
+
21
+ Also note that this extension does not override any of the existing tags, and should be backwards-compatible with 0.8 and earlier.
22
+
23
+
24
+ [1]: http://radiantcms.org/ "Radiant: Content management simplified"
25
+ [2]: http://hardcoreforkingaction.com "Hard-core forking action: a low frequency, web-tech blog by Benny Degezelle"
data/Rakefile ADDED
@@ -0,0 +1,137 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gem|
4
+ gem.name = "radiant-group_children-extension"
5
+ gem.summary = %Q{Group Children Extension for Radiant CMS}
6
+ gem.description = %Q{Adds the ability to iterate over a page's children per n items.}
7
+ gem.email = "benny@gorilla-webdesign.be"
8
+ gem.homepage = "http://github.com/jomz/radiant-group_children-extension"
9
+ gem.authors = ["Benny Degezelle"]
10
+ gem.add_dependency 'radiant', ">=0.9.1"
11
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
12
+ end
13
+ rescue LoadError
14
+ puts "Jeweler (or a dependency) not available. This is only required if you plan to package group_children as a gem."
15
+ end
16
+
17
+ # In rails 1.2, plugins aren't available in the path until they're loaded.
18
+ # Check to see if the rspec plugin is installed first and require
19
+ # it if it is. If not, use the gem version.
20
+
21
+ # Determine where the RSpec plugin is by loading the boot
22
+ unless defined? RADIANT_ROOT
23
+ ENV["RAILS_ENV"] = "test"
24
+ case
25
+ when ENV["RADIANT_ENV_FILE"]
26
+ require File.dirname(ENV["RADIANT_ENV_FILE"]) + "/boot"
27
+ when File.dirname(__FILE__) =~ %r{vendor/radiant/vendor/extensions}
28
+ require "#{File.expand_path(File.dirname(__FILE__) + "/../../../../../")}/config/boot"
29
+ else
30
+ require "#{File.expand_path(File.dirname(__FILE__) + "/../../../")}/config/boot"
31
+ end
32
+ end
33
+
34
+ require 'rake'
35
+ require 'rake/rdoctask'
36
+ require 'rake/testtask'
37
+
38
+ rspec_base = File.expand_path(RADIANT_ROOT + '/vendor/plugins/rspec/lib')
39
+ $LOAD_PATH.unshift(rspec_base) if File.exist?(rspec_base)
40
+ require 'spec/rake/spectask'
41
+ require 'cucumber'
42
+ require 'cucumber/rake/task'
43
+
44
+ # Cleanup the RADIANT_ROOT constant so specs will load the environment
45
+ Object.send(:remove_const, :RADIANT_ROOT)
46
+
47
+ extension_root = File.expand_path(File.dirname(__FILE__))
48
+
49
+ task :default => :spec
50
+ task :stats => "spec:statsetup"
51
+
52
+ desc "Run all specs in spec directory"
53
+ Spec::Rake::SpecTask.new(:spec) do |t|
54
+ t.spec_opts = ['--options', "\"#{extension_root}/spec/spec.opts\""]
55
+ t.spec_files = FileList['spec/**/*_spec.rb']
56
+ end
57
+
58
+ task :features => 'spec:integration'
59
+
60
+ namespace :spec do
61
+ desc "Run all specs in spec directory with RCov"
62
+ Spec::Rake::SpecTask.new(:rcov) do |t|
63
+ t.spec_opts = ['--options', "\"#{extension_root}/spec/spec.opts\""]
64
+ t.spec_files = FileList['spec/**/*_spec.rb']
65
+ t.rcov = true
66
+ t.rcov_opts = ['--exclude', 'spec', '--rails']
67
+ end
68
+
69
+ desc "Print Specdoc for all specs"
70
+ Spec::Rake::SpecTask.new(:doc) do |t|
71
+ t.spec_opts = ["--format", "specdoc", "--dry-run"]
72
+ t.spec_files = FileList['spec/**/*_spec.rb']
73
+ end
74
+
75
+ [:models, :controllers, :views, :helpers].each do |sub|
76
+ desc "Run the specs under spec/#{sub}"
77
+ Spec::Rake::SpecTask.new(sub) do |t|
78
+ t.spec_opts = ['--options', "\"#{extension_root}/spec/spec.opts\""]
79
+ t.spec_files = FileList["spec/#{sub}/**/*_spec.rb"]
80
+ end
81
+ end
82
+
83
+ desc "Run the Cucumber features"
84
+ Cucumber::Rake::Task.new(:integration) do |t|
85
+ t.fork = true
86
+ t.cucumber_opts = ['--format', (ENV['CUCUMBER_FORMAT'] || 'pretty')]
87
+ # t.feature_pattern = "#{extension_root}/features/**/*.feature"
88
+ t.profile = "default"
89
+ end
90
+
91
+ # Setup specs for stats
92
+ task :statsetup do
93
+ require 'code_statistics'
94
+ ::STATS_DIRECTORIES << %w(Model\ specs spec/models)
95
+ ::STATS_DIRECTORIES << %w(View\ specs spec/views)
96
+ ::STATS_DIRECTORIES << %w(Controller\ specs spec/controllers)
97
+ ::STATS_DIRECTORIES << %w(Helper\ specs spec/views)
98
+ ::CodeStatistics::TEST_TYPES << "Model specs"
99
+ ::CodeStatistics::TEST_TYPES << "View specs"
100
+ ::CodeStatistics::TEST_TYPES << "Controller specs"
101
+ ::CodeStatistics::TEST_TYPES << "Helper specs"
102
+ ::STATS_DIRECTORIES.delete_if {|a| a[0] =~ /test/}
103
+ end
104
+
105
+ namespace :db do
106
+ namespace :fixtures do
107
+ desc "Load fixtures (from spec/fixtures) into the current environment's database. Load specific fixtures using FIXTURES=x,y"
108
+ task :load => :environment do
109
+ require 'active_record/fixtures'
110
+ ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
111
+ (ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir.glob(File.join(RAILS_ROOT, 'spec', 'fixtures', '*.{yml,csv}'))).each do |fixture_file|
112
+ Fixtures.create_fixtures('spec/fixtures', File.basename(fixture_file, '.*'))
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end
118
+
119
+ desc 'Generate documentation for the group_children extension.'
120
+ Rake::RDocTask.new(:rdoc) do |rdoc|
121
+ rdoc.rdoc_dir = 'rdoc'
122
+ rdoc.title = 'GroupChildrenExtension'
123
+ rdoc.options << '--line-numbers' << '--inline-source'
124
+ rdoc.rdoc_files.include('README')
125
+ rdoc.rdoc_files.include('lib/**/*.rb')
126
+ end
127
+
128
+ # For extensions that are in transition
129
+ desc 'Test the group_children extension.'
130
+ Rake::TestTask.new(:test) do |t|
131
+ t.libs << 'lib'
132
+ t.pattern = 'test/**/*_test.rb'
133
+ t.verbose = true
134
+ end
135
+
136
+ # Load any custom rakefiles for extension
137
+ Dir[File.dirname(__FILE__) + '/tasks/*.rake'].sort.each { |f| require f }
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,3 @@
1
+ ---
2
+ en:
3
+ group children: Group Children
data/config/routes.rb ADDED
@@ -0,0 +1,5 @@
1
+ ActionController::Routing::Routes.draw do |map|
2
+ # map.namespace :admin, :member => { :remove => :get } do |admin|
3
+ # admin.resources :group_children
4
+ # end
5
+ end
data/cucumber.yml ADDED
@@ -0,0 +1 @@
1
+ default: --format progress features --tags ~@proposed,~@in_progress
@@ -0,0 +1,16 @@
1
+ # Sets up the Rails environment for Cucumber
2
+ ENV["RAILS_ENV"] = "test"
3
+ # Extension root
4
+ extension_env = File.expand_path(File.dirname(__FILE__) + '/../../../../../config/environment')
5
+ require extension_env+'.rb'
6
+
7
+ Dir.glob(File.join(RADIANT_ROOT, "features", "**", "*.rb")).each {|step| require step}
8
+
9
+ Cucumber::Rails::World.class_eval do
10
+ include Dataset
11
+ datasets_directory "#{RADIANT_ROOT}/spec/datasets"
12
+ Dataset::Resolver.default = Dataset::DirectoryResolver.new("#{RADIANT_ROOT}/spec/datasets", File.dirname(__FILE__) + '/../../spec/datasets', File.dirname(__FILE__) + '/../datasets')
13
+ self.datasets_database_dump_path = "#{Rails.root}/tmp/dataset"
14
+
15
+ # dataset :group_children
16
+ end
@@ -0,0 +1,14 @@
1
+ def path_to(page_name)
2
+ case page_name
3
+
4
+ when /the homepage/i
5
+ root_path
6
+
7
+ when /login/i
8
+ login_path
9
+ # Add more page name => path mappings here
10
+
11
+ else
12
+ raise "Can't find mapping from \"#{page_name}\" to a path."
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ class GroupChildrenExtension < Radiant::Extension
2
+ version "1.0"
3
+ description "Adds the ability to iterate over a page's children per n items."
4
+ url "http://github.com/jomz/radiant-group_children-extension"
5
+
6
+ def activate
7
+ Array.send :include, ArrayHack
8
+ Page.send :include, GroupTags
9
+ end
10
+ end
data/lib/array_hack.rb ADDED
@@ -0,0 +1,9 @@
1
+ module ArrayHack
2
+ def % len
3
+ inject([]) do |groups, value|
4
+ groups << [] if !groups.last || groups.last.size >= len
5
+ groups.last << value
6
+ groups
7
+ end
8
+ end
9
+ end
data/lib/group_tags.rb ADDED
@@ -0,0 +1,111 @@
1
+ module GroupTags
2
+ include Radiant::Taggable
3
+
4
+ class TagError < StandardError; end
5
+
6
+ desc %{
7
+ Cycles through groups of child pages. Inside this tag, children:each is available again.
8
+ Takes the same parameters as a normal children:each, except for pagination.
9
+
10
+ *Usage:*
11
+
12
+ <pre><code><r:children:grouped [per="number"] [limit="number"]
13
+ [by="published_at|updated_at|created_at|slug|title|keywords|description"]
14
+ [order="asc|desc"]
15
+ [status="draft|reviewed|published|hidden|all"]
16
+ >
17
+ <div class="container"><r:children:each>...</r:children:each></div>
18
+ </r:children:grouped>
19
+ </code></pre>
20
+ }
21
+ tag 'children:grouped' do |tag|
22
+ options = children_find_options(tag)
23
+ group_size = tag.attr['per'].to_i || 2
24
+ tag.locals.groups = tag.locals.children.all(options) % group_size
25
+ result = []
26
+ tag.locals.groups.each_with_index do |group, i|
27
+ tag.locals.children = group
28
+ tag.locals.first_group = i == 0
29
+ tag.locals.last_group = i == tag.locals.groups.length - 1
30
+ result << tag.expand
31
+ end
32
+ result
33
+ end
34
+
35
+ tag 'children:grouped:if_first' do |tag|
36
+ tag.expand if tag.locals.first_group
37
+ end
38
+
39
+ tag 'children:grouped:if_last' do |tag|
40
+ tag.expand if tag.locals.last_group
41
+ end
42
+
43
+ tag 'children:grouped:children' do |tag|
44
+ tag.expand
45
+ end
46
+
47
+ tag 'children:grouped:children:each' do |tag|
48
+ result = []
49
+ tag.locals.children.each_with_index do |page, i|
50
+ tag.locals.child = page
51
+ tag.locals.page = page
52
+ tag.locals.first_child = i == 0
53
+ tag.locals.last_child = i == tag.locals.children.length - 1
54
+ result << tag.expand
55
+ end
56
+ result
57
+ end
58
+
59
+ tag 'children:grouped:children:each:if_first' do |tag|
60
+ tag.expand if tag.locals.first_child
61
+ end
62
+
63
+ tag 'children:grouped:children:each:if_last' do |tag|
64
+ tag.expand if tag.locals.last_child
65
+ end
66
+
67
+ private
68
+ def children_find_options(tag)
69
+ attr = tag.attr.symbolize_keys
70
+
71
+ options = {}
72
+
73
+ [:limit, :offset].each do |symbol|
74
+ if number = attr[symbol]
75
+ if number =~ /^\d{1,4}$/
76
+ options[symbol] = number.to_i
77
+ else
78
+ raise TagError.new("`#{symbol}' attribute of `each' tag must be a positive number between 1 and 4 digits")
79
+ end
80
+ end
81
+ end
82
+
83
+ by = (attr[:by] || 'published_at').strip
84
+ order = (attr[:order] || 'asc').strip
85
+ order_string = ''
86
+ if self.attributes.keys.include?(by)
87
+ order_string << by
88
+ else
89
+ raise TagError.new("`by' attribute of `each' tag must be set to a valid field name")
90
+ end
91
+ if order =~ /^(asc|desc)$/i
92
+ order_string << " #{$1.upcase}"
93
+ else
94
+ raise TagError.new(%{`order' attribute of `each' tag must be set to either "asc" or "desc"})
95
+ end
96
+ options[:order] = order_string
97
+
98
+ status = (attr[:status] || ( dev?(tag.globals.page.request) ? 'all' : 'published')).downcase
99
+ unless status == 'all'
100
+ stat = Status[status]
101
+ unless stat.nil?
102
+ options[:conditions] = ["(virtual = ?) and (status_id = ?)", false, stat.id]
103
+ else
104
+ raise TagError.new(%{`status' attribute of `each' tag must be set to a valid status})
105
+ end
106
+ else
107
+ options[:conditions] = ["virtual = ?", false]
108
+ end
109
+ options
110
+ end
111
+ end
@@ -0,0 +1,55 @@
1
+ namespace :radiant do
2
+ namespace :extensions do
3
+ namespace :group_children do
4
+
5
+ desc "Runs the migration of the Group Children extension"
6
+ task :migrate => :environment do
7
+ require 'radiant/extension_migrator'
8
+ if ENV["VERSION"]
9
+ GroupChildrenExtension.migrator.migrate(ENV["VERSION"].to_i)
10
+ Rake::Task['db:schema:dump'].invoke
11
+ else
12
+ GroupChildrenExtension.migrator.migrate
13
+ Rake::Task['db:schema:dump'].invoke
14
+ end
15
+ end
16
+
17
+ desc "Copies public assets of the Group Children to the instance public/ directory."
18
+ task :update => :environment do
19
+ is_svn_or_dir = proc {|path| path =~ /\.svn/ || File.directory?(path) }
20
+ puts "Copying assets from GroupChildrenExtension"
21
+ Dir[GroupChildrenExtension.root + "/public/**/*"].reject(&is_svn_or_dir).each do |file|
22
+ path = file.sub(GroupChildrenExtension.root, '')
23
+ directory = File.dirname(path)
24
+ mkdir_p RAILS_ROOT + directory, :verbose => false
25
+ cp file, RAILS_ROOT + path, :verbose => false
26
+ end
27
+ unless GroupChildrenExtension.root.starts_with? RAILS_ROOT # don't need to copy vendored tasks
28
+ puts "Copying rake tasks from GroupChildrenExtension"
29
+ local_tasks_path = File.join(RAILS_ROOT, %w(lib tasks))
30
+ mkdir_p local_tasks_path, :verbose => false
31
+ Dir[File.join GroupChildrenExtension.root, %w(lib tasks *.rake)].each do |file|
32
+ cp file, local_tasks_path, :verbose => false
33
+ end
34
+ end
35
+ end
36
+
37
+ desc "Syncs all available translations for this ext to the English ext master"
38
+ task :sync => :environment do
39
+ # The main translation root, basically where English is kept
40
+ language_root = GroupChildrenExtension.root + "/config/locales"
41
+ words = TranslationSupport.get_translation_keys(language_root)
42
+
43
+ Dir["#{language_root}/*.yml"].each do |filename|
44
+ next if filename.match('_available_tags')
45
+ basename = File.basename(filename, '.yml')
46
+ puts "Syncing #{basename}"
47
+ (comments, other) = TranslationSupport.read_file(filename, basename)
48
+ words.each { |k,v| other[k] ||= words[k] } # Initializing hash variable as empty if it does not exist
49
+ other.delete_if { |k,v| !words[k] } # Remove if not defined in en.yml
50
+ TranslationSupport.write_file(filename, basename, comments, other)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,6 @@
1
+ --colour
2
+ --format
3
+ progress
4
+ --loadby
5
+ mtime
6
+ --reverse
@@ -0,0 +1,36 @@
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
+ Dataset::Resolver.default << (File.dirname(__FILE__) + "/datasets")
15
+
16
+ if File.directory?(File.dirname(__FILE__) + "/matchers")
17
+ Dir[File.dirname(__FILE__) + "/matchers/*.rb"].each {|file| require file }
18
+ end
19
+
20
+ Spec::Runner.configure do |config|
21
+ # config.use_transactional_fixtures = true
22
+ # config.use_instantiated_fixtures = false
23
+ # config.fixture_path = RAILS_ROOT + '/spec/fixtures'
24
+
25
+ # You can declare fixtures for each behaviour like this:
26
+ # describe "...." do
27
+ # fixtures :table_a, :table_b
28
+ #
29
+ # Alternatively, if you prefer to declare them only once, you can
30
+ # do so here, like so ...
31
+ #
32
+ # config.global_fixtures = :table_a, :table_b
33
+ #
34
+ # If you declare global fixtures, be aware that they will be declared
35
+ # for all of your examples, even those that don't use them.
36
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: radiant-group_children-extension
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Benny Degezelle
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-23 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: radiant
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 57
30
+ segments:
31
+ - 0
32
+ - 9
33
+ - 1
34
+ version: 0.9.1
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: Adds the ability to iterate over a page's children per n items.
38
+ email: benny@gorilla-webdesign.be
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - README.md
45
+ files:
46
+ - README.md
47
+ - Rakefile
48
+ - VERSION
49
+ - config/locales/en.yml
50
+ - config/routes.rb
51
+ - cucumber.yml
52
+ - features/support/env.rb
53
+ - features/support/paths.rb
54
+ - group_children_extension.rb
55
+ - lib/array_hack.rb
56
+ - lib/group_tags.rb
57
+ - lib/tasks/group_children_extension_tasks.rake
58
+ - spec/spec.opts
59
+ - spec/spec_helper.rb
60
+ has_rdoc: true
61
+ homepage: http://github.com/jomz/radiant-group_children-extension
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options:
66
+ - --charset=UTF-8
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ hash: 3
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ requirements: []
88
+
89
+ rubyforge_project:
90
+ rubygems_version: 1.3.7
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: Group Children Extension for Radiant CMS
94
+ test_files:
95
+ - spec/spec_helper.rb