Skip to content

Commit

Permalink
Require persisted? in ActiveModel::Lint and remove new_record? and de…
Browse files Browse the repository at this point in the history
…stroyed? methods. ActionPack does not care if the resource is new or if it was destroyed, it cares only if it's persisted somewhere or not.
  • Loading branch information
josevalim committed Feb 21, 2010
1 parent 9dd67fc commit 250c809
Show file tree
Hide file tree
Showing 20 changed files with 112 additions and 125 deletions.
3 changes: 1 addition & 2 deletions actionpack/lib/action_controller/polymorphic_routes.rb
Expand Up @@ -92,8 +92,7 @@ def polymorphic_url(record_or_hash_or_array, options = {})
inflection = if options[:action].to_s == "new"
args.pop
:singular
elsif (record.respond_to?(:new_record?) && record.new_record?) ||
(record.respond_to?(:destroyed?) && record.destroyed?)
elsif (record.respond_to?(:persisted?) && !record.persisted?)
args.pop
:plural
elsif record.is_a?(Class)
Expand Down
4 changes: 2 additions & 2 deletions actionpack/lib/action_view/helpers/active_model_helper.rb
Expand Up @@ -80,13 +80,13 @@ def form(record_name, options = {})
record = convert_to_model(record)

options = options.symbolize_keys
options[:action] ||= record.new_record? ? "create" : "update"
options[:action] ||= record.persisted? ? "update" : "create"
action = url_for(:action => options[:action], :id => record)

submit_value = options[:submit_value] || options[:action].gsub(/[^\w]/, '').capitalize

contents = form_tag({:action => action}, :method =>(options[:method] || 'post'), :enctype => options[:multipart] ? 'multipart/form-data': nil)
contents.safe_concat hidden_field(record_name, :id) unless record.new_record?
contents.safe_concat hidden_field(record_name, :id) if record.persisted?
contents.safe_concat all_input_tags(record, record_name, options)
yield contents if block_given?
contents.safe_concat submit_tag(submit_value)
Expand Down
17 changes: 8 additions & 9 deletions actionpack/lib/action_view/helpers/form_helper.rb
Expand Up @@ -316,14 +316,13 @@ def form_for(record_or_name_or_array, *args, &proc)

def apply_form_for_options!(object_or_array, options) #:nodoc:
object = object_or_array.is_a?(Array) ? object_or_array.last : object_or_array

object = convert_to_model(object)

html_options =
if object.respond_to?(:new_record?) && object.new_record?
{ :class => dom_class(object, :new), :id => dom_id(object), :method => :post }
else
if object.respond_to?(:persisted?) && object.persisted?
{ :class => dom_class(object, :edit), :id => dom_id(object, :edit), :method => :put }
else
{ :class => dom_class(object, :new), :id => dom_id(object), :method => :post }
end

options[:html] ||= {}
Expand Down Expand Up @@ -1150,7 +1149,7 @@ def objectify_options(options)

def submit_default_value
object = @object.respond_to?(:to_model) ? @object.to_model : @object
key = object ? (object.new_record? ? :create : :update) : :submit
key = object ? (object.persisted? ? :update : :create) : :submit

model = if object.class.respond_to?(:model_name)
object.class.model_name.human
Expand All @@ -1176,7 +1175,7 @@ def fields_for_with_nested_attributes(association_name, args, block)
association = args.shift
association = association.to_model if association.respond_to?(:to_model)

if association.respond_to?(:new_record?)
if association.respond_to?(:persisted?)
association = [association] if @object.send(association_name).is_a?(Array)
elsif !association.is_a?(Array)
association = @object.send(association_name)
Expand All @@ -1195,13 +1194,13 @@ def fields_for_with_nested_attributes(association_name, args, block)
def fields_for_nested_model(name, object, options, block)
object = object.to_model if object.respond_to?(:to_model)

if object.new_record?
@template.fields_for(name, object, options, &block)
else
if object.persisted?
@template.fields_for(name, object, options) do |builder|
block.call(builder)
@template.concat builder.hidden_field(:id) unless builder.emitted_hidden_id?
end
else
@template.fields_for(name, object, options, &block)
end
end

Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_view/helpers/url_helper.rb
Expand Up @@ -63,7 +63,7 @@ def default_url_options(*args) #:nodoc:
# # => /testing/jump/#tax&ship
#
# <%= url_for(Workshop.new) %>
# # relies on Workshop answering a new_record? call (and in this case returning true)
# # relies on Workshop answering a persisted? call (and in this case returning false)
# # => /workshops
#
# <%= url_for(@workshop) %>
Expand Down
4 changes: 2 additions & 2 deletions actionpack/test/controller/mime_responds_test.rb
Expand Up @@ -513,7 +513,7 @@ def respond; @controller.render :text => "respond #{format}"; end
protected

def resource
Customer.new("david", 13)
Customer.new("david", request.delete? ? nil : 13)
end

def _render_js(js, options)
Expand Down Expand Up @@ -717,7 +717,7 @@ def test_using_resource_for_delete_with_html_redirects_on_failure
delete :using_resource
assert_equal "text/html", @response.content_type
assert_equal 302, @response.status
assert_equal "http://www.example.com/customers/13", @response.location
assert_equal "http://www.example.com/customers", @response.location
end
end

Expand Down
18 changes: 9 additions & 9 deletions actionpack/test/controller/redirect_test.rb
Expand Up @@ -6,14 +6,14 @@ class WorkshopsController < ActionController::Base
class Workshop
extend ActiveModel::Naming
include ActiveModel::Conversion
attr_accessor :id, :new_record
attr_accessor :id

def initialize(id, new_record)
@id, @new_record = id, new_record
def initialize(id)
@id = id
end

def new_record?
@new_record
def persisted?
id.present?
end

def to_s
Expand Down Expand Up @@ -88,11 +88,11 @@ def redirect_to_back
end

def redirect_to_existing_record
redirect_to Workshop.new(5, false)
redirect_to Workshop.new(5)
end

def redirect_to_new_record
redirect_to Workshop.new(5, true)
redirect_to Workshop.new(nil)
end

def redirect_to_nil
Expand Down Expand Up @@ -239,11 +239,11 @@ def test_redirect_to_record

get :redirect_to_existing_record
assert_equal "http://test.host/workshops/5", redirect_to_url
assert_redirected_to Workshop.new(5, false)
assert_redirected_to Workshop.new(5)

get :redirect_to_new_record
assert_equal "http://test.host/workshops", redirect_to_url
assert_redirected_to Workshop.new(5, true)
assert_redirected_to Workshop.new(nil)
end
end

Expand Down
40 changes: 12 additions & 28 deletions actionpack/test/lib/controller/fake_models.rb
Expand Up @@ -6,14 +6,6 @@ class Customer < Struct.new(:name, :id)

undef_method :to_json

def to_key
id ? [id] : nil
end

def to_param
id.to_s
end

def to_xml(options={})
if options[:builder]
options[:builder].name name
Expand All @@ -31,8 +23,8 @@ def errors
[]
end

def destroyed?
false
def persisted?
id.present?
end
end

Expand All @@ -47,12 +39,8 @@ class Question < Struct.new(:name, :id)
extend ActiveModel::Naming
include ActiveModel::Conversion

def to_key
id ? [id] : nil
end

def to_param
id.to_s
def persisted?
id.present?
end
end

Expand All @@ -67,16 +55,12 @@ class Post < Struct.new(:title, :author_name, :body, :secret, :written_on, :cost

alias_method :secret?, :secret

def to_key
id ? [id] : nil
end

def new_record=(boolean)
@new_record = boolean
def persisted=(boolean)
@persisted = boolean
end

def new_record?
@new_record
def persisted?
@persisted
end

attr_accessor :author
Expand All @@ -98,7 +82,7 @@ class Comment
def initialize(id = nil, post_id = nil); @id, @post_id = id, post_id end
def to_key; id ? [id] : nil end
def save; @id = 1; @post_id = 1 end
def new_record?; @id.nil? end
def persisted?; @id.present? end
def to_param; @id; end
def name
@id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}"
Expand All @@ -118,7 +102,7 @@ class Tag
def initialize(id = nil, post_id = nil); @id, @post_id = id, post_id end
def to_key; id ? [id] : nil end
def save; @id = 1; @post_id = 1 end
def new_record?; @id.nil? end
def persisted?; @id.present? end
def to_param; @id; end
def value
@id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}"
Expand All @@ -138,7 +122,7 @@ class CommentRelevance
def initialize(id = nil, comment_id = nil); @id, @comment_id = id, comment_id end
def to_key; id ? [id] : nil end
def save; @id = 1; @comment_id = 1 end
def new_record?; @id.nil? end
def persisted?; @id.present? end
def to_param; @id; end
def value
@id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}"
Expand All @@ -154,7 +138,7 @@ class TagRelevance
def initialize(id = nil, tag_id = nil); @id, @tag_id = id, tag_id end
def to_key; id ? [id] : nil end
def save; @id = 1; @tag_id = 1 end
def new_record?; @id.nil? end
def persisted?; @id.present? end
def to_param; @id; end
def value
@id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}"
Expand Down
4 changes: 2 additions & 2 deletions actionpack/test/template/active_model_helper_test.rb
Expand Up @@ -64,7 +64,7 @@ def full_messages() [ "Author name can't be empty" ] end
}.new
end

def @post.new_record?() true end
def @post.persisted?() false end
def @post.to_param() nil end

def @post.column_for_attribute(attr_name)
Expand Down Expand Up @@ -155,7 +155,7 @@ def test_form_with_string

silence_warnings do
class << @post
def new_record?() false end
def persisted?() true end
def to_param() id end
def id() 1 end
end
Expand Down
8 changes: 4 additions & 4 deletions actionpack/test/template/atom_feed_helper_test.rb
Expand Up @@ -4,8 +4,8 @@ class Scroll < Struct.new(:id, :to_param, :title, :body, :updated_at, :created_a
extend ActiveModel::Naming
include ActiveModel::Conversion

def new_record?
true
def persisted?
false
end
end

Expand Down Expand Up @@ -34,7 +34,7 @@ class ScrollsController < ActionController::Base
feed.updated((@scrolls.first.created_at))
for scroll in @scrolls
feed.entry(scroll, :url => "/otherstuff/" + scroll.to_param, :updated => Time.utc(2007, 1, scroll.id)) do |entry|
feed.entry(scroll, :url => "/otherstuff/" + scroll.to_param.to_s, :updated => Time.utc(2007, 1, scroll.id)) do |entry|
entry.title(scroll.title)
entry.content(scroll.body, :type => 'html')
Expand All @@ -55,7 +55,7 @@ class ScrollsController < ActionController::Base
end
for scroll in @scrolls
feed.entry(scroll, :url => "/otherstuff/" + scroll.to_param, :updated => Time.utc(2007, 1, scroll.id)) do |entry|
feed.entry(scroll, :url => "/otherstuff/" + scroll.to_param.to_s, :updated => Time.utc(2007, 1, scroll.id)) do |entry|
entry.title(scroll.title)
entry.content(scroll.body, :type => 'html')
end
Expand Down
13 changes: 3 additions & 10 deletions actionpack/test/template/form_helper_test.rb
Expand Up @@ -53,6 +53,7 @@ def @post.id; 123; end
def @post.id_before_type_cast; 123; end
def @post.to_param; '123'; end

@post.persisted = true
@post.title = "Hello World"
@post.author_name = ""
@post.body = "Back to the hill and over it again!"
Expand Down Expand Up @@ -529,7 +530,7 @@ def test_form_for_with_nil_index_option_override
def test_submit_with_object_as_new_record_and_locale_strings
old_locale, I18n.locale = I18n.locale, :submit

def @post.new_record?() true; end
@post.persisted = false
form_for(:post, @post) do |f|
concat f.submit
end
Expand Down Expand Up @@ -1363,7 +1364,7 @@ def test_form_for_with_existing_object

def test_form_for_with_new_object
post = Post.new
post.new_record = true
post.persisted = false
def post.id() nil end

form_for(post) do |f| end
Expand All @@ -1373,37 +1374,29 @@ def post.id() nil end
end

def test_form_for_with_existing_object_in_list
@post.new_record = false
@comment.save

form_for([@post, @comment]) {}

expected = %(<form action="#{comment_path(@post, @comment)}" class="edit_comment" id="edit_comment_1" method="post"><div style="margin:0;padding:0;display:inline"><input name="_method" type="hidden" value="put" /></div></form>)
assert_dom_equal expected, output_buffer
end

def test_form_for_with_new_object_in_list
@post.new_record = false

form_for([@post, @comment]) {}

expected = %(<form action="#{comments_path(@post)}" class="new_comment" id="new_comment" method="post"></form>)
assert_dom_equal expected, output_buffer
end

def test_form_for_with_existing_object_and_namespace_in_list
@post.new_record = false
@comment.save

form_for([:admin, @post, @comment]) {}

expected = %(<form action="#{admin_comment_path(@post, @comment)}" class="edit_comment" id="edit_comment_1" method="post"><div style="margin:0;padding:0;display:inline"><input name="_method" type="hidden" value="put" /></div></form>)
assert_dom_equal expected, output_buffer
end

def test_form_for_with_new_object_and_namespace_in_list
@post.new_record = false

form_for([:admin, @post, @comment]) {}

expected = %(<form action="#{admin_comments_path(@post)}" class="new_comment" id="new_comment" method="post"></form>)
Expand Down
1 change: 1 addition & 0 deletions actionpack/test/template/record_tag_helper_test.rb
Expand Up @@ -18,6 +18,7 @@ class RecordTagHelperTest < ActionView::TestCase
def setup
super
@post = Post.new
@post.persisted = true
end

def test_content_tag_for
Expand Down

1 comment on commit 250c809

@jpartogi
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the to_key method in ActiveModel::Conversion is for?

Please sign in to comment.