Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: jseifer/find-param
base: master
...
head fork: tylerhunt/find-param
compare: master
Checking mergeability… Don’t worry, you can still create the pull request.
  • 2 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
View
8 lib/find_by_param.rb
@@ -40,9 +40,11 @@ def initialize_parameter(param, source, options={})
using = options.delete(:using)
define_method(:set_param) do
- value = read_attribute(source)
- value = using.respond_to?(:call) ? using.call(value) : value.downcase.gsub(/[^\w]+/, '-')
- write_attribute(param, value)
+ unless read_attribute(param) && read_attribute(param).any?
+ value = read_attribute(source)
+ value = using.respond_to?(:call) ? using.call(value) : value.downcase.gsub(/[^\w]+/, '-')
+ write_attribute(param, value)
+ end
end
private :set_param
View
12 test/find_by_param_test.rb
@@ -68,6 +68,18 @@ def test_initializes_param_on_create
assert_equal 'a-test-post', blog_post.slug
end
+ def test_initializes_param_on_create_should_not_override_a_set_value
+ Post.find_param(:slug, :initialize_with => :title)
+ blog_post = Post.create(:title => 'A Test Post', :slug => 'test-post')
+ assert_equal 'test-post', blog_post.slug
+ end
+
+ def test_initializes_param_on_create_should_override_an_empty_string
+ Post.find_param(:slug, :initialize_with => :title)
+ blog_post = Post.create(:title => 'A Test Post', :slug => '')
+ assert_equal 'a-test-post', blog_post.slug
+ end
+
def test_initializes_param_on_create_using_a_custom_initializer
Post.find_param(:slug, :initialize_with => :title, :using => Proc.new { |value| value.upcase.gsub(/\s+/, '_') })
blog_post = Post.create(:title => 'A Test Post')
View
2  test/test_helper.rb
@@ -17,7 +17,7 @@
ActiveRecord::Base.send(:extend, FindByParam::ClassMethods)
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :dbfile => ':memory:')
-ActiveRecord::Base.logger = Logger.new(STDOUT)
+#ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define(:version => 1) do
create_table :posts do |t|

No commit comments for this range

Something went wrong with that request. Please try again.