Topic: How to make something that works like a "approved" and "not approved".

I'm pretty clueless, well I have a clue on this, but not enough to develop it.

Sorry for it to be a newbie question, but stick with me on this.


There are links and categories, links belongs to category, category has many links.

Okay.

So what I want to be able to do is have every link that is created to be unapproved. And it can only be approved if edited through the editing/update interface. (Which I'm planning only an admin can access).

If anyone has an idea on how I can do this, (just the verified/unverified process)

Then please let me know.

Thanks!

http://danielfischer.com - Personal Web-Technology-Blog, Los Angeles.

Re: How to make something that works like a "approved" and "not approved".

Let's see, there's a few approaches, the simplest would probably be to add a "approved" (or "isApproved" depending on your convention preference) field to the link model.  Use the on_create callback to set self.approved=false.  I haven't played with before_update enough to know if you could do that to flip  an approved link that was editted back to unapproved.  (Actually I know you could, but I don't know if it would also flip a non-dirty, but re-saved item also...)

If you need more states, then you could go with a link_status table and have links belong_to :link_status.  (New, approved, updated, tagged_for_edit) if you want to deal with new and updated differently, and want to be able to mark something as "unapproved and needs to be fixed before I look at it again")

My RoR journey  -- thoughts on learning RoR and lessons learned in applying TDD and agile practices.

Re: How to make something that works like a "approved" and "not approved".

the easiest i can think of is like so:

Create a column in Link called approved_at and set to datetime ( i always use datetime instead of a bool column because it gives you time relation as well as true/false)

In your link model add the following method

def before_create
  approved_at = nil  #this will force all new links to be unapproved when they are first created
end

Then create your admin with a checkbox for approval and have it stamp the date on that field when you save.  Done


hope that helps


--jake

---------------------------------------------------------------------
Rails Development - Agile rails consulting and development for startups on a budget
Flvorful Blog - Some ramblings
Flvorful's Open Source Projects - Some handy hacks for your rails projects.

Re: How to make something that works like a "approved" and "not approved".

NielsenE wrote:

Let's see, there's a few approaches, the simplest would probably be to add a "approved" (or "isApproved" depending on your convention preference) field to the link model.  Use the on_create callback to set self.approved=false.  I haven't played with before_update enough to know if you could do that to flip  an approved link that was editted back to unapproved.  (Actually I know you could, but I don't know if it would also flip a non-dirty, but re-saved item also...)

If you need more states, then you could go with a link_status table and have links belong_to :link_status.  (New, approved, updated, tagged_for_edit) if you want to deal with new and updated differently, and want to be able to mark something as "unapproved and needs to be fixed before I look at it again")

There is no "on_create" it seems. Only the following:

after_create
after_destroy
after_save
after_update
after_validation
after_validation_on_create
after_validation_on_update
before_create
before_destroy
before_save
before_update
before_validation
before_validation_on_create
before_validation_on_update

http://www.rubyonrails.org/api/classes/ … ml#M000792

Also, this on create self.approved=false would go in the model right?


edit: i ended up doing this and I think it works. (have to fix my edit def first, it's giving me errors) But on the column I saw "false" on a new link I created.

My new link  model

class Link < ActiveRecord::Base
  belongs_to :category
  validates_presence_of :title, :category, :description, :name, :email,
                                :on => :create, :message => "can't be blank"
                               
  validates_format_of :url, :with => /^(https?:\/\/)?([^\/]+)(\/.+)?$/i
 
  def after_validation_on_create
    self.approved = false
  end
end

Last edited by DFischer (2006-12-29 19:48:56)

http://danielfischer.com - Personal Web-Technology-Blog, Los Angeles.