public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/technoweenie/mephisto.git
Click here to lend your support to: mephisto and make a donation at www.pledgie.com !
Added new liquid vars {{ site.latest_articles }} and {{ 
site.latest_comments }} (see CHANGELOG)

git-svn-id: http://svn.techno-weenie.net/projects/mephisto/trunk@2198 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Sun Sep 17 19:50:20 -0700 2006
commit  82cd4997a0d6b48243e66fa6ba7de46baeb9c08e
tree    0372501dce99fe5207127005442499f4f05dec28
parent  9beb8db52d83155e5126634234ffa68438ea0de4
...
1
2
 
 
 
3
4
5
...
1
2
3
4
5
6
7
8
0
@@ -1,5 +1,8 @@
0
 * SVN *
0
 
0
+# Added new liquid vars {{ site.latest_articles }} and {{ site.latest_comments }},
0
+ as well as filters like {{ site | latest_articles: 5 }} or {{ site | latest_comments: 5 }}
0
+
0
 * move liquid drops/filters to app/drops and app/filters
0
 
0
 * add multi-asset uploading capabilities.
...
28
29
30
31
 
 
32
33
34
...
28
29
30
 
31
32
33
34
35
0
@@ -28,7 +28,8 @@ class Admin::AssetsController < Admin::BaseController
0
     flash[:notice] = @assets.size == 1 ? "'#{CGI.escapeHTML @assets.first.title}' was uploaded." : "#{@assets.size} assets were uploaded."
0
     @assets.size.zero? ? render(:action => 'new') : redirect_to(assets_path)
0
   rescue ActiveRecord::RecordInvalid
0
- render(:action => 'new')
0
+ breakpoint
0
+ render :action => 'new'
0
   end
0
 
0
   def update
...
21
22
23
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
26
27
...
21
22
23
 
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
0
@@ -21,7 +21,23 @@ class SiteDrop < BaseDrop
0
   def home_section
0
     find_section ''
0
   end
0
-
0
+
0
+ def latest_articles(limit = nil)
0
+ return @articles if @articles && limit == @source.articles_per_page
0
+ articles = returning @source.articles.find_by_date(:limit => (limit || @source.articles_per_page)) do |articles|
0
+ articles.collect! &:to_liquid
0
+ end
0
+ limit == @source.articles_per_page ? (@articles = articles) : articles
0
+ end
0
+
0
+ def latest_comments(limit = nil)
0
+ return @comments if @comments && limit == @source.articles_per_page
0
+ comments = returning @source.comments.find(:all, :limit => (limit || @source.articles_per_page)) do |comments|
0
+ comments.collect! &:to_liquid
0
+ end
0
+ limit == @source.articles_per_page ? (@comments = comments) : comments
0
+ end
0
+
0
   def find_section(path)
0
     @section_index ||= {}
0
     return @section_index[path] if @section_index[path]
...
130
131
132
133
134
 
 
135
136
137
138
139
 
 
 
 
140
141
142
...
130
131
132
 
 
133
134
135
136
137
138
139
140
141
142
143
144
145
146
0
@@ -130,13 +130,17 @@ module Filters
0
     @context['site'].find_child_sections(path)
0
   end
0
 
0
- def latest_articles(section, limit = nil)
0
- section.latest_articles(limit || section['articles_per_page'])
0
+ def latest_articles(site_or_section, limit = nil)
0
+ site_or_section.latest_articles(limit || site_or_section['articles_per_page'])
0
   end
0
 
0
   def latest_article(section)
0
     latest_articles(section, 1).first
0
   end
0
+
0
+ def latest_comments(site, limit = nil)
0
+ site.latest_comments(limit || site['articles_per_page'])
0
+ end
0
 
0
   def assign_to(value, name)
0
     @context[name] = value ; nil
...
51
52
53
54
55
56
57
 
 
 
 
58
59
60
61
62
63
64
65
66
67
68
69
70
...
51
52
53
 
 
 
 
54
55
56
57
58
59
 
 
 
 
 
 
 
 
60
61
62
0
@@ -51,20 +51,12 @@ class Article < Content
0
   end
0
 
0
   class << self
0
- def approve
0
- comment = @article.unapproved_comments.find(params[:comment])
0
- comment.approved = true
0
- comment.save
0
+ def find_by_date(options = {})
0
+ find(:all, { :order => 'contents.published_at desc',
0
+ :conditions => ['contents.published_at <= ? AND contents.published_at IS NOT NULL', Time.now.utc] } \
0
+ .merge(options))
0
     end
0
     
0
- def unapprove
0
- comment = @article.comments.find(params[:comment])
0
- comment.approved = false
0
- comment.save
0
- end
0
- end
0
-
0
- class << self
0
     def find_all_in_month(year, month, options = {})
0
       find(:all, options.merge(:order => 'contents.published_at DESC', :conditions => ["contents.published_at <= ? AND contents.published_at BETWEEN ? AND ?",
0
         Time.now.utc, *Time.delta(year.to_i, month.to_i)]))
...
49
50
51
 
52
53
54
...
76
77
78
 
 
 
 
 
 
 
 
 
 
 
 
 
79
80
 
 
81
82
83
...
49
50
51
52
53
54
55
...
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
 
94
95
96
97
98
0
@@ -49,6 +49,7 @@ class Asset < ActiveRecord::Base
0
   before_validation_on_create :set_site_from_parent
0
   validates_presence_of :site_id
0
   validates_as_attachment
0
+ validate :rename_unique_filename
0
 
0
   def full_filename(thumbnail = nil)
0
     file_system_path = (thumbnail ? thumbnail_class : self).attachment_options[:file_system_path]
0
@@ -76,8 +77,22 @@ class Asset < ActiveRecord::Base
0
   end
0
 
0
   protected
0
+ def rename_unique_filename
0
+ if (@old_filename || new_record?) && errors.empty? && site_id && filename
0
+ i = 1
0
+ pieces = filename.split('.')
0
+ ext = pieces.size == 1 ? nil : pieces.pop
0
+ base = pieces * '.'
0
+ while File.exists?(full_filename)
0
+ write_attribute :filename, base + "_#{i}#{".#{ext}" if ext}"
0
+ i += 1
0
+ end
0
+ end
0
+ end
0
+
0
     def permalink
0
- pieces = [site.host, created_at.year, created_at.month, created_at.day]
0
+ date = created_at || Time.now.utc
0
+ pieces = [site.host, date.year, date.month, date.day]
0
       pieces.shift unless Site.multi_sites_enabled
0
       pieces * '/'
0
     end
...
9
10
11
12
13
14
15
16
17
18
19
20
...
9
10
11
 
 
 
 
 
 
12
13
14
0
@@ -9,12 +9,6 @@ class Section < ActiveRecord::Base
0
   belongs_to :site
0
   has_many :assigned_sections, :dependent => :delete_all
0
   has_many :articles, :order => 'position', :through => :assigned_sections do
0
- def find_by_date(options = {})
0
- find(:all, { :order => 'contents.published_at desc',
0
- :conditions => ['contents.published_at <= ? AND contents.published_at IS NOT NULL', Time.now.utc] } \
0
- .merge(options))
0
- end
0
-
0
     def find_by_position(options = {})
0
       find(:first, { :conditions => ['contents.published_at <= ? AND contents.published_at IS NOT NULL', Time.now.utc] } \
0
         .merge(options))
...
29
30
31
32
 
33
34
35
 
 
36
37
38
...
29
30
31
 
32
33
34
35
36
37
38
39
40
0
@@ -29,10 +29,12 @@ class Site < ActiveRecord::Base
0
           end
0
         end
0
       
0
- find :first, :conditions => conditions
0
+ find :first, :conditions => conditions, :order => 'published_at desc'
0
     end
0
   end
0
   
0
+ has_many :comments, :order => 'comments.created_at desc'
0
+
0
   has_many :events
0
   
0
   has_many :cached_pages
...
20
21
22
23
24
25
26
27
28
 
29
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
32
33
...
158
159
160
 
 
 
 
 
 
 
 
161
...
20
21
22
 
 
 
 
 
 
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
...
172
173
174
175
176
177
178
179
180
181
182
183
0
@@ -20,14 +20,28 @@ class AssetTest < Test::Unit::TestCase
0
   
0
   def test_should_upload_file
0
     process_upload
0
- now = Time.now.utc
0
- assert_file_exists File.join(ASSET_PATH, now.year.to_s, now.month.to_s, now.day.to_s, 'logo.png')
0
- if Object.const_defined?(:Magick)
0
- assert_file_exists File.join(ASSET_PATH, now.year.to_s, now.month.to_s, now.day.to_s, 'logo_thumb.png')
0
- assert_file_exists File.join(ASSET_PATH, now.year.to_s, now.month.to_s, now.day.to_s, 'logo_tiny.png')
0
- end
0
+ assert_assets_exist :logo
0
   end
0
 
0
+ def test_should_rename_non_unique_filename
0
+ asset = process_upload
0
+ assert_equal 'logo.png', asset.filename
0
+ asset = process_upload
0
+ assert_equal 'logo_1.png', asset.filename
0
+ assert_assets_exist :logo_1
0
+ end
0
+
0
+ def test_should_rename_non_unique_filename_when_renaming
0
+ now = Time.now.utc
0
+ asset = process_upload
0
+ assert_equal 'logo.png', asset.filename
0
+ asset = process_upload(:filename => 'logo_reloaded.png')
0
+ assert_assets_exist :logo_reloaded, now
0
+
0
+ asset.update_attributes :filename => 'logo.png'
0
+ assert_file_exists File.join(ASSET_PATH, now.year.to_s, now.month.to_s, now.day.to_s, "logo_1.png")
0
+ end
0
+
0
   def test_should_upload_file_in_multi_sites_mode
0
     Site.multi_sites_enabled = true
0
     process_upload
0
@@ -158,4 +172,12 @@ class AssetTest < Test::Unit::TestCase
0
       assert_valid a
0
       a
0
     end
0
+
0
+ def assert_assets_exist(filename, created_at = Time.now.utc)
0
+ assert_file_exists File.join(ASSET_PATH, created_at.year.to_s, created_at.month.to_s, created_at.day.to_s, "#{filename}.png")
0
+ if Object.const_defined?(:Magick)
0
+ assert_file_exists File.join(ASSET_PATH, created_at.year.to_s, created_at.month.to_s, created_at.day.to_s, "#{filename}_thumb.png")
0
+ assert_file_exists File.join(ASSET_PATH, created_at.year.to_s, created_at.month.to_s, created_at.day.to_s, "#{filename}_tiny.png")
0
+ end
0
+ end
0
 end
...
65
66
67
68
 
 
69
70
71
...
79
80
81
 
 
 
 
 
 
 
 
 
 
82
83
...
65
66
67
 
68
69
70
71
72
...
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
0
@@ -65,7 +65,8 @@ context "Drop Filters" do
0
   include Filters
0
 
0
   def setup
0
- @context = {'site' => sites(:first).to_liquid, 'section' => sections(:about).to_liquid}
0
+ @site = sites(:first).to_liquid
0
+ @context = {'site' => @site, 'section' => sections(:about).to_liquid}
0
   end
0
 
0
   specify "should find section by path" do
0
@@ -79,4 +80,14 @@ context "Drop Filters" do
0
     assert_models_equal [contents(:welcome), contents(:another)], latest_articles(section, 2).collect(&:source)
0
     assert_equal contents(:welcome), latest_article(section).source
0
   end
0
+
0
+ specify "should find latest articles by site" do
0
+ assert_models_equal [contents(:welcome), contents(:about), contents(:site_map), contents(:another)], latest_articles(@site).collect(&:source)
0
+ assert_models_equal [contents(:welcome), contents(:about)], latest_articles(@site, 2).collect(&:source)
0
+ end
0
+
0
+ specify "should find latest comments by site" do
0
+ assert_models_equal [contents(:welcome_comment)], latest_comments(@site).collect(&:source)
0
+ assert_models_equal [contents(:welcome_comment)], latest_comments(@site, 1).collect(&:source)
0
+ end
0
 end
0
\ No newline at end of file
...
51
52
53
 
 
 
 
 
 
 
 
 
 
54
55
56
...
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
0
@@ -51,6 +51,16 @@ class SiteDropTest < Test::Unit::TestCase
0
     assert_equal sections(:about), @site.find_section('about').source
0
   end
0
 
0
+ def test_should_find_latest_articles
0
+ assert_models_equal [contents(:welcome), contents(:about), contents(:site_map), contents(:another)], @site.latest_articles.collect(&:source)
0
+ assert_models_equal [contents(:welcome), contents(:about)], @site.latest_articles(2).collect(&:source)
0
+ end
0
+
0
+ def test_should_find_latest_comments
0
+ assert_models_equal [contents(:welcome_comment)], @site.latest_comments.collect(&:source)
0
+ assert_models_equal [contents(:welcome_comment)], @site.latest_comments(1).collect(&:source)
0
+ end
0
+
0
   def test_liquid_keys
0
     [:host, :subtitle, :title, :articles_per_page].each do |attr|
0
       assert_equal sites(:first).send(attr), @site.before_method(attr)

Comments

    No one has commented yet.