This repository has been archived by the owner on Aug 15, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
added slugs to the blog plugin, added ability to modify a page that g…
…ets shown after each blog post
- Loading branch information
Josh Adams
committed
Dec 27, 2008
1 parent
2e7d812
commit 18d6417
Showing
205 changed files
with
47,272 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
vendor/plugins/ansuz_blog/app/views/admin/blog_posts/edit.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
vendor/plugins/ansuz_blog/app/views/admin/blog_posts/show.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
vendor/plugins/ansuz_blog/app/views/articles/_article.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<div class='blog_post' id='<%= dom_id(article) -%>'> | ||
<h2><%=h article.title -%></h2> | ||
<div class='contents'> | ||
<%= article.contents -%> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<%= render :partial => 'articles/article', :locals => { :article => @blog_post } %> | ||
<div class='post-blog-post'> | ||
<%= render_special "post_blog_post" %> | ||
</div> | ||
<%= link_to "All Blog Posts", articles_path -%> |
9 changes: 9 additions & 0 deletions
9
vendor/plugins/ansuz_blog/db/migrate/002_add_url_to_blog_posts.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class AddUrlToBlogPosts < ActiveRecord::Migration | ||
def self.up | ||
add_column :blog_posts, :url, :string | ||
end | ||
|
||
def self.down | ||
remove_column :blog_posts, :url | ||
end | ||
end |
11 changes: 7 additions & 4 deletions
11
vendor/plugins/has_settings/db/migrate/20081220_create_has_settings_settings.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.DS_Store | ||
doc | ||
github-test.rb | ||
test/*.sqlite3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2008 Lucky Sneaks | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
= Stringex | ||
|
||
Some [hopefully] useful extensions to Ruby's String class. It is made up of three libraries: ActsAsUrl, Unidecoder, and StringExtensions. | ||
|
||
== ActsAsUrl | ||
|
||
This library is designed to create URI-friendly representations of an attribute, for use in generating urls from your attributes. Basic usage is just calling the method: | ||
|
||
acts_as_url :title | ||
|
||
which will populate the <tt>url</tt> attribute on the object with the converted contents of the <tt>title</tt> attribute. This behavior can be customized by adding the following options to the arguments of the <tt>acts_as_url</tt> method: | ||
|
||
<tt>:url_attribute</tt>:: The name of the attribute to use for storing the generated url string. | ||
Default is <tt>:url</tt> | ||
<tt>:scope</tt>:: The name of model attribute to scope unique urls to. There is no default here. | ||
<tt>:only_when_blank</tt>:: If true, the url generation will only happen when <tt>:url_attribute</tt> is | ||
blank. Default is false (meaning url generation will happen always) | ||
<tt>:sync_url</tt>:: If set to true, the url field will be updated when changes are made to the | ||
attribute it is based on. Default is false. | ||
|
||
In order to use the generated url attribute, you will probably want to override <tt>to_param</tt> like so, in your Model: | ||
|
||
def to_param | ||
url # or whatever you set :url_attribute to | ||
end | ||
|
||
Routing called via named routes like <tt>foo_path(@foo)</tt> will automatically use the url. In your controllers you will need to call <tt>Foo.find_by_url(params[:id])</tt> instead of the regular find. Don't look for <tt>params[:url]</tt> unless you set it explicitly in the routing, <tt>to_param</tt> will generate <tt>params[:id]</tt>. | ||
|
||
Unlike other permalink solutions, ActsAsUrl doesn't rely on Iconv (which is inconsistent across platforms and doesn't provide great transliteration as is) but instead uses a transliteration scheme (see the code for Unidecoder) which produces much better results for Unicode characters. It also mixes in some custom helpers to translate common characters into a more URI-friendly format rather than just dump them completely. Examples: | ||
|
||
# A simple prelude | ||
"simple English".to_url => "simple-english" | ||
"it's nothing at all".to_url => "its-nothing-at-all" | ||
"rock & roll".to_url => "rock-and-roll" | ||
|
||
# Let's show off | ||
"$12 worth of Ruby power".to_url => "12-dollars-worth-of-ruby-power" | ||
"10% off if you act now".to_url => "10-percent-off-if-you-act-now" | ||
|
||
# You don't even wanna trust Iconv for this next part | ||
"kick it en Français".to_url => "kick-it-en-francais" | ||
"rock it Español style".to_url => "rock-it-espanol-style" | ||
"tell your readers 你好".to_url => "tell-your-readers-ni-hao" | ||
|
||
Compare those results with the ones produced on my Intel Mac by a leading permalink plugin: | ||
|
||
"simple English" # => "simple-english" | ||
"it's nothing at all" # => "it-s-nothing-at-all" | ||
"rock & roll" # => "rock-roll" | ||
|
||
"$12 worth of Ruby power" # => "12-worth-of-ruby-power" | ||
"10% off if you act now" # => "10-off-if-you-act-now" | ||
|
||
"kick it en Français" # => "kick-it-en-francais" | ||
"rock it Español style" # => "rock-it-espan-ol-style" | ||
"tell your readers 你好" # => "tell-your-readers" | ||
|
||
Not so great, actually. | ||
|
||
Note: No offense is intended to the author[s] of whatever plugins might produce such results. It's not your faults Iconv sucks. | ||
|
||
== Unidecoder | ||
|
||
This library converts Unicode [and accented Ascii] characters to their plain-text Ascii equivalents. This is a port of Perl's Unidecode and provides eminently superior and more reliable results than Iconv. (Seriously, Iconv... A plague on both your houses! [sic]) | ||
|
||
You probably won't ever need to run Unidecoder by itself. StringExtensions adds String#to_ascii which wraps all of Unidecoder's functionality. For anyone interested, details of the implementation can be read about in the original implementation of Text::Unidecode[http://interglacial.com/~sburke/tpj/as_html/tpj22.html]. Extensive examples can be found in the tests. | ||
|
||
== StringExtensions | ||
|
||
A collection of extensions on Ruby's String class. Please see the documentation for string_extensions module for more information. There's not much to explain about them really. | ||
|
||
== Thanks & Acknowledgements | ||
|
||
If it's not obvious, some of the code for ActsAsUrl is based on Rick Olsen's permalink_fu[http://svn.techno-weenie.net/projects/plugins/permalink_fu/] plugin. Unidecoder is a Ruby port of Sean Burke's Text::Unidecode[http://interglacial.com/~sburke/tpj/as_html/tpj22.html] module for Perl. And, finally, the bulk of strip_html_tags[link:classes/LuckySneaks/StringExtensions.html#M000005] in StringExtensions was stolen from Tobias Lütke's Regex in Typo[http://typosphere.org/]. | ||
|
||
copyright (c) 2008 Lucky Sneaks, released under the MIT license |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'rake' | ||
require 'rake/testtask' | ||
require 'rake/rdoctask' | ||
|
||
desc 'Default: run unit tests.' | ||
task :default => [:refresh_db, :test] | ||
|
||
desc 'Remove old sqlite file' | ||
task :refresh_db do | ||
`rm -f #{File.dirname(__FILE__)}/test/acts_as_url.sqlite3` | ||
end | ||
|
||
desc 'Test the stringex plugin.' | ||
Rake::TestTask.new(:test) do |t| | ||
t.libs << 'lib' | ||
t.pattern = 'test/**/*_test.rb' | ||
t.verbose = true | ||
end | ||
|
||
desc 'Generate documentation for the stringex plugin.' | ||
Rake::RDocTask.new(:rdoc) do |rdoc| | ||
rdoc.rdoc_dir = 'doc' | ||
rdoc.title = 'Stringex: A String Extension Pack' | ||
rdoc.options << '--line-numbers' << '--inline-source' | ||
rdoc.options << '--charset' << 'utf-8' | ||
rdoc.rdoc_files.include('README.rdoc') | ||
rdoc.rdoc_files.include('lib/**/*.rb') | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require "stringex" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
module LuckySneaks | ||
module ActsAsUrl # :nodoc: | ||
def self.included(base) | ||
base.extend ClassMethods | ||
end | ||
|
||
module ClassMethods # :doc: | ||
# Creates a callback to automatically create an url-friendly representation | ||
# of the <tt>attribute</tt> argument. Example: | ||
# | ||
# act_as_url :title | ||
# | ||
# will use the string contents of the <tt>title</tt> attribute | ||
# to create the permalink. <strong>Note:</strong> you can also use a non-database-backed | ||
# method to supply the string contents for the permalink. Just use that method's name | ||
# as the argument as you would an attribute. | ||
# | ||
# The default attribute <tt>acts_as_url</tt> uses to save the permalink is <tt>url</tt> | ||
# but this can be changed in the options hash. Available options are: | ||
# | ||
# <tt>:url_attribute</tt>:: The name of the attribute to use for storing the generated url string. | ||
# Default is <tt>:url</tt> | ||
# <tt>:scope</tt>:: The name of model attribute to scope unique urls to. There is no default here. | ||
# <tt>:only_when_blank</tt>:: If true, the url generation will only happen when <tt>:url_attribute</tt> is | ||
# blank. Default is false (meaning url generation will happen always) | ||
# <tt>:sync_url</tt>:: If set to true, the url field will be updated when changes are made to the | ||
# attribute it is based on. Default is false. | ||
def acts_as_url(attribute, options = {}) | ||
cattr_accessor :attribute_to_urlify | ||
cattr_accessor :scope_for_url | ||
cattr_accessor :url_attribute # The attribute on the DB | ||
cattr_accessor :only_when_blank | ||
|
||
if options[:sync_url] | ||
before_validation :ensure_unique_url | ||
else | ||
before_validation_on_create :ensure_unique_url | ||
end | ||
|
||
self.attribute_to_urlify = attribute | ||
self.scope_for_url = options[:scope] | ||
self.url_attribute = options[:url_attribute] || "url" | ||
self.only_when_blank = options[:only_when_blank] || false | ||
end | ||
|
||
# Initialize the url fields for the records that need it. Designed for people who add | ||
# <tt>acts_as_url</tt> support once there's already development/production data they'd | ||
# like to keep around. | ||
# | ||
# Note: This method can get very expensive, very fast. If you're planning on using this | ||
# on a large selection, you will get much better results writing your own version with | ||
# using pagination. | ||
def initialize_urls | ||
find(:all, :conditions => {self.url_attribute => nil}).each do |instance| | ||
instance.send :ensure_unique_url | ||
instance.save | ||
end | ||
end | ||
end | ||
|
||
private | ||
def ensure_unique_url | ||
url_attribute = self.class.url_attribute | ||
base_url = self.send(url_attribute) | ||
base_url = self.send(self.class.attribute_to_urlify).to_s.to_url if base_url.blank? || !self.only_when_blank | ||
conditions = ["#{url_attribute} LIKE ?", base_url+'%'] | ||
unless new_record? | ||
conditions.first << " and id != ?" | ||
conditions << id | ||
end | ||
if self.class.scope_for_url | ||
conditions.first << " and #{self.class.scope_for_url} = ?" | ||
conditions << send(self.class.scope_for_url) | ||
end | ||
url_owners = self.class.find(:all, :conditions => conditions) | ||
if url_owners.size > 0 | ||
n = 1 | ||
while url_owners.detect{|u| u.send(url_attribute) == "#{base_url}-#{n}"} | ||
n = n.succ | ||
end | ||
write_attribute url_attribute, "#{base_url}-#{n}" | ||
else | ||
write_attribute url_attribute, base_url | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.