radiant-snippets-extension 1.0.2 → 1.1.0.alpha

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ class Admin::SnippetFilesController < Admin::ResourceController
2
+ def show;end
3
+ end
@@ -0,0 +1,49 @@
1
+ class SnippetFile < Struct.new(:name, :content)
2
+ class << self
3
+ def find_by_name(name)
4
+ if file(name)
5
+ self.new(name, self.read(name))
6
+ end
7
+ end
8
+ alias find find_by_name
9
+
10
+ def all
11
+ Dir.glob([root, '/','*',suffix].join('')).collect{ |file_path|
12
+ name = File.basename(file_path, suffix)
13
+ content = File.read(file_path)
14
+ self.new(name, content)
15
+ }
16
+ end
17
+
18
+ def root
19
+ @root ||= Rails.root.to_s + '/app/templates/snippets'
20
+ end
21
+
22
+ def suffix
23
+ '.radius'
24
+ end
25
+
26
+ def path(name)
27
+ [root, '/', name, suffix].join('')
28
+ end
29
+
30
+ def file(name, collection=Dir.glob(path(name)))
31
+ @files ||= {}
32
+ return @files[name] if @files[name]
33
+ @files[name] = collection.first
34
+ end
35
+
36
+ def read(name, reader=File)
37
+ reader.read(file(name))
38
+ end
39
+ end
40
+
41
+ def to_param
42
+ name
43
+ end
44
+
45
+ def updated_at
46
+ time = File.mtime(self.class.file(name))
47
+ I18n.localize(time, :format => :timestamp)
48
+ end
49
+ end
@@ -0,0 +1,24 @@
1
+ class SnippetFinder
2
+ class << self
3
+ def find(id)
4
+ find_map('find',id)
5
+ end
6
+
7
+ def find_by_name(name)
8
+ find_map('find_by_name', name)
9
+ end
10
+
11
+ def finder_types
12
+ [SnippetFile, Snippet]
13
+ end
14
+
15
+ private
16
+
17
+ def find_map(meth, *args)
18
+ finder_types.find{|type|
19
+ found = type.send(meth, *args)
20
+ return found if found
21
+ }
22
+ end
23
+ end
24
+ end
@@ -36,7 +36,7 @@ module SnippetTags
36
36
 
37
37
  snippet = @snippet_cache[name]
38
38
  unless snippet
39
- snippet = Snippet.find_by_name(name)
39
+ snippet = SnippetFinder.find_by_name(name)
40
40
  @snippet_cache[name] = snippet
41
41
  end
42
42
  snippet
@@ -0,0 +1,26 @@
1
+ - @page_title = t('snippet_files') + ' - ' + default_page_title
2
+
3
+ .outset
4
+ = render_region :top
5
+ %table.index#snippet_files
6
+ %thead
7
+ %tr
8
+ - render_region :thead do |thead|
9
+ - thead.title_header do
10
+ %th.name= t('snippet')
11
+ %tbody
12
+ - if @snippet_files.any?
13
+ - @snippet_files.each do |snippet|
14
+ %tr[snippet]
15
+ - render_region :tbody, :locals => {:snippet => snippet} do |tbody|
16
+ - tbody.title_cell do
17
+ %td.name
18
+ = link_to image('snippet', :alt => '') + ' ' + snippet.name, admin_snippet_file_url(snippet)
19
+ - else
20
+ %tr
21
+ %td.empty{:colspan => admin.snippet.index.tbody.length}= t('no_snippet_files')
22
+
23
+ - render_region :bottom do |bottom|
24
+ - bottom.new_button do
25
+ #actions
26
+ = pagination_for(@snippet_files)
@@ -0,0 +1,19 @@
1
+ - @page_title = model.name + ' ' + t('snippet_file') + ' - ' + default_page_title
2
+
3
+ - render_region :main do |main|
4
+ - main.header do
5
+ %h1= model.name
6
+ .form_area
7
+ - render_region :display_content do |display|
8
+ - display.title do
9
+ %p.title
10
+ = t('name') + ':'
11
+ = model.name
12
+ - display.content do
13
+ %p.content
14
+ = t('body')
15
+ %br/
16
+ ~ text_area_tag :content, model.content, :class => "textarea large", :style => "width: 95%"
17
+ - render_region :bottom do |bottom|
18
+ - bottom.timestamp do
19
+ = model.updated_at
@@ -2,6 +2,8 @@
2
2
  en:
3
3
  snippet: 'Snippet'
4
4
  snippets: 'Snippets'
5
+ snippet_files: 'Snippet Files'
6
+ snippet_file: 'Snippet File'
5
7
  config:
6
8
  defaults:
7
9
  snippet:
@@ -10,6 +12,7 @@ en:
10
12
  edit_snippet: 'Edit Snippet'
11
13
  new_snippet: 'New Snippet'
12
14
  no_snippets: 'No Snippets'
15
+ no_snippet_files: 'No Snippet Files'
13
16
  remove_snippet: 'Remove Snippet'
14
17
  text:
15
18
  snippets:
data/config/routes.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  ActionController::Routing::Routes.draw do |map|
2
2
  map.namespace :admin, :member => { :remove => :get } do |admin|
3
3
  admin.resources :snippets
4
+ admin.resources :snippet_files, :only => [:index, :show]
4
5
  end
5
6
  end
@@ -1,5 +1,5 @@
1
1
  module RadiantSnippetsExtension
2
- VERSION = "1.0.2"
2
+ VERSION = "1.1.0.alpha"
3
3
  SUMMARY = "Snippets for Radiant CMS"
4
4
  DESCRIPTION = "Makes Radiant better by adding snippets!"
5
5
  URL = "http://github.com/radiant"
@@ -19,8 +19,10 @@ class SnippetsExtension < Radiant::Extension
19
19
  end
20
20
 
21
21
  Radiant::AdminUI.class_eval do
22
- attr_accessor :snippet
22
+ attr_accessor :snippet, :snippet_file
23
+
23
24
  alias_method :snippets, :snippet
25
+ alias_method :snippet_files, :snippet_file
24
26
 
25
27
  def load_default_snippet_regions
26
28
  OpenStruct.new.tap do |snippet|
@@ -38,14 +40,32 @@ class SnippetsExtension < Radiant::Extension
38
40
  snippet.new = snippet.edit
39
41
  end
40
42
  end
43
+
44
+ def load_default_snippet_file_regions
45
+ OpenStruct.new.tap do |snippet|
46
+ snippet.show = Radiant::AdminUI::RegionSet.new do |edit|
47
+ edit.main.concat %w{ header }
48
+ edit.display_content.concat %w{ title content }
49
+ edit.bottom.concat %w{ timestamp }
50
+ end
51
+ snippet.index = Radiant::AdminUI::RegionSet.new do |index|
52
+ index.top.concat %w{}
53
+ index.thead.concat %w{title_header}
54
+ index.tbody.concat %w{title_cell}
55
+ index.bottom.concat %w{new_button}
56
+ end
57
+ end
58
+ end
41
59
  end
42
60
 
43
- admin.snippet ||= Radiant::AdminUI.load_default_snippet_regions
61
+ admin.snippet ||= Radiant::AdminUI.load_default_snippet_regions
62
+ admin.snippet_file ||= Radiant::AdminUI.load_default_snippet_file_regions
44
63
 
45
- UserActionObserver.instance.send :add_observer!, Snippet
64
+ UserActionObserver.instance.send :add_observer!, ::Snippet
46
65
 
47
66
  tab 'Design' do
48
67
  add_item "Snippets", "/admin/snippets"
68
+ add_item "Snippet Files", "/admin/snippet_files"
49
69
  end
50
70
 
51
71
  end
@@ -0,0 +1 @@
1
+ <h1>Hello</h1>
@@ -0,0 +1,39 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../snippets_spec_helper.rb')
2
+
3
+ describe SnippetFile do
4
+
5
+ let(:fixture_file){
6
+ spec_directory_path = File.expand_path(File.dirname(__FILE__))
7
+ fixture_directory_path = spec_directory_path + "/fixtures"
8
+ Dir.glob(fixture_directory_path + 'snippet_template.radius').first
9
+ }
10
+
11
+ it 'has a root in the Rails path' do
12
+ SnippetFile.root.should == Rails.root.to_s + '/app/templates/snippets'
13
+ end
14
+
15
+ it 'has a file suffix' do
16
+ SnippetFile.suffix.should == '.radius'
17
+ end
18
+
19
+ it "returns a snippet file from the found file" do
20
+ mock_file = OpenStruct.new(:name => 'snippet_template')
21
+ SnippetFile.stub!(:file).with('snippet_template').and_return(mock_file)
22
+ SnippetFile.stub!(:read).with('snippet_template').and_return('content')
23
+ SnippetFile.find_by_name('snippet_template').should be_a(SnippetFile)
24
+ end
25
+
26
+ it 'has a file found by name' do
27
+ SnippetFile.file(name).should == fixture_file
28
+ end
29
+
30
+ it 'returns contents of a named snippet file' do
31
+ reader = mock('reader')
32
+ reader.stub!(:read).and_return('content')
33
+ SnippetFile.read(name, reader).should == 'content'
34
+ end
35
+
36
+ subject{ SnippetFile.new('test','details')}
37
+ its(:name){ should == 'test' }
38
+ its(:content){ should == 'details'}
39
+ end
@@ -0,0 +1,20 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../snippets_spec_helper.rb')
2
+
3
+ class TestSnippetFinder; end
4
+ class AlternateSnippetFinder; end
5
+
6
+ describe 'SnippetFinder' do
7
+ it 'finds a snippet by name in the first finder type' do
8
+ SnippetFinder.stub!(:finder_types).and_return([TestSnippetFinder, AlternateSnippetFinder])
9
+ TestSnippetFinder.should_receive(:find_by_name).and_return(OpenStruct.new(:name => 'test_snippet'))
10
+
11
+ SnippetFinder.find_by_name('test_snippet')
12
+ end
13
+ it 'finds a snippet by name in the next finder type after no result from the previous' do
14
+ SnippetFinder.stub!(:finder_types).and_return([TestSnippetFinder, AlternateSnippetFinder])
15
+ TestSnippetFinder.stub!(:find_by_name).and_return(nil)
16
+ AlternateSnippetFinder.should_receive(:find_by_name).and_return(OpenStruct.new(:name => 'alternate_snippet'))
17
+
18
+ SnippetFinder.find_by_name('alternate_snippet')
19
+ end
20
+ end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: radiant-snippets-extension
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
5
- prerelease:
4
+ version: 1.1.0.alpha
5
+ prerelease: 6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jim Gay
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-01 00:00:00.000000000 Z
12
+ date: 2012-08-27 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Makes Radiant better by adding snippets!
15
15
  email:
@@ -18,9 +18,15 @@ executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
+ - app/controllers/admin/snippet_files_controller.rb
21
22
  - app/controllers/admin/snippets_controller.rb
22
23
  - app/helpers/admin/snippets_helper.rb
23
24
  - app/models/snippet.rb
25
+ - app/models/snippet_file.rb
26
+ - app/models/snippet_finder.rb
27
+ - app/models/snippet_tags.rb
28
+ - app/views/admin/snippet_files/index.html.haml
29
+ - app/views/admin/snippet_files/show.html.haml
24
30
  - app/views/admin/snippets/_form.html.haml
25
31
  - app/views/admin/snippets/edit.html.haml
26
32
  - app/views/admin/snippets/index.html.haml
@@ -37,7 +43,6 @@ files:
37
43
  - features/support/env.rb
38
44
  - features/support/paths.rb
39
45
  - lib/radiant-snippets-extension.rb
40
- - lib/snippet_tags.rb
41
46
  - lib/tasks/snippets_extension_tasks.rake
42
47
  - radiant-snippets-extension.gemspec
43
48
  - Rakefile
@@ -47,8 +52,11 @@ files:
47
52
  - spec/ci/script
48
53
  - spec/controllers/admin/snippets_controller_spec.rb
49
54
  - spec/datasets/snippets_dataset.rb
55
+ - spec/fixtures/snippet_template.radius
50
56
  - spec/lib/radiant/admin_ui_spec.rb
51
57
  - spec/models/page_spec.rb
58
+ - spec/models/snippet_file_spec.rb
59
+ - spec/models/snippet_finder_spec.rb
52
60
  - spec/models/snippet_spec.rb
53
61
  - spec/models/user_action_observer_spec.rb
54
62
  - spec/snippets_spec_helper.rb
@@ -68,9 +76,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
68
76
  required_rubygems_version: !ruby/object:Gem::Requirement
69
77
  none: false
70
78
  requirements:
71
- - - ! '>='
79
+ - - ! '>'
72
80
  - !ruby/object:Gem::Version
73
- version: '0'
81
+ version: 1.3.1
74
82
  requirements: []
75
83
  rubyforge_project:
76
84
  rubygems_version: 1.8.11
@@ -82,8 +90,11 @@ test_files:
82
90
  - spec/ci/script
83
91
  - spec/controllers/admin/snippets_controller_spec.rb
84
92
  - spec/datasets/snippets_dataset.rb
93
+ - spec/fixtures/snippet_template.radius
85
94
  - spec/lib/radiant/admin_ui_spec.rb
86
95
  - spec/models/page_spec.rb
96
+ - spec/models/snippet_file_spec.rb
97
+ - spec/models/snippet_finder_spec.rb
87
98
  - spec/models/snippet_spec.rb
88
99
  - spec/models/user_action_observer_spec.rb
89
100
  - spec/snippets_spec_helper.rb