Moderated model editing in Rails2 - ruby-on-rails

I want to allow users to edit a model, adding a picture, url and/or phone number to it. However I don't want these changes to show up until they are confirmed by a moderator. I do however want to allow the users to create new entries without moderation, but they cannot include the picture, url, and/or phone number (they can include basic details like the name and surname). Or better, they can include the picture etc. but at the start only the basic information will show up until the picture etc. are accepted by a moderator.
I'm using Rails 2.3.5.

If you want to keep things simple, you can just have a moderated? on your model. Then have your moderated have complete edit access to un-moderated models.
And when rendered your model, don't show the picture, url, or phone number.
I like Andrew's suggestion above for state-machine and versions, but if you want to keep it simple, a nice flag will do exactly that

It sounds like you need to record the revision history of your model. You can use a plugin like Vestal Versions to do this.
When you update your model, you need to set a moderated flag on it. This could be done with a simple checkbox, or a more advanced state machine plugin.

Related

How to make logo interchangeable, Ruby on Rails

I have created a custom CMS Rails app for a local company, and had the request to allow the logo to be interchangeable for the holidays. For example, they talked about having choices of different logos for each holiday.
I am imaging a radio button looking something like this
Normal
Christmas
Christmas1
Easter
Easter1
Thanksgiving
4th of July
etc
So, does anyone have any idea how I would implement this, or have any experience with it?
I have an admin panel for them and I am thinking of adding a section that has the radio buttons mentioned above, and depending on what one is set a variable changes values and displays a different logo from the images folder, but not sure if that's the route to go.
Thanks for any help.
Edit: Sounds like I have the right idea, can I get some advice from you experts on how you would go about implementing this? I'm thinking of having a logo model where they can upload the image to, but how would I implement that into the view to allow them to pick?
Don't confuse two different things you will store in the database: the list of logos, and the setting of the current logo. The former will consist of a model and a table. The latter could be a simple foreign key pointing to the correct entry in that table.
You should also have a new controller since you plan to let them manage the list of logos. For image uploading check out Carrierwave which has comprehensive examples.

Best way to have default user settings in Ruby on Rails?

I'm currently working on a RoR application where users are able to register and have a list of (lets say 20) image-formats. These image-formats have their own model. The admin of the website is able to add or remove formats. The users should be able to activate certain formats or deactivate.
I was thinking of just adding the 20 formats to the database for each user, but that wouldn't be flexible if the admin chooses to delete or add one. Their should be a more maintainable way?
Your explanation isn't very detailed. Especially I'm missing the "default" settings from the question.
First thing (easy one):
The Admin can create an entry for each format he wants to allow on the website. So there should be exactly one such entry in the table
Users can select from these formats several (up to a limit?) Why do they need to specially choose them? Can't they just use everything the admin allows? What would happen to uploaded images if the admin deletes a format?
Anyway, best would be a has_and_belongs_to_many relationship between the User and ImageFormat. Or another Model (UserImageFormat) that builds this bridge if any additional data is necessary.
For the case, that the Admin removes some formats, you can handle this in several ways. Either you add an "active" flag to the format record and then check for this each time you use the model or you actually just delete this record (in this case, make sure your associations are set with :dependent => :destroy so all the records that connect users to formats get destroyed too. (Write tests to ensure this)
Maybe you can find some gems or plugins that help with that. This article may give you some more help. Maybe some of the authentication plugins can add access control. But I think your need (user can in some way influence what he can use/access) could be too special, since most gems for this kind of work will more likely address an admin centered access control.

How to implement a "flag for moderator attention" feature on a Comment model in a Rails app?

I have a Comment model in my app but am encountering a lot of problematic postings that I have to remove manually.
What I want to do is add a "flag for moderator attention" feature so that users of the app have the ability to remove a comment from view without my need to review all content in the app.
I'm thinking that after a comment has been flagged three times, I will remove it from view automatically and then when I have a chance to review these postings I will decide whether to allow them or permanently remove them from view.
What I'm having trouble with is how to implement this.
Should I have a separate table that records all items that have been flagged?
Or should I have a "flag count" field as part of the Comment table that keeps track of how many times a comment has been flagged?
A separate table would allow me to keep track of detailed information about the flagging actions - who is flagging, which IP they are flagging from, etc. This is what I'm leaning towards.
But perhaps a gem or plugin already exists that does this type of thing?
I don't know of any plugin. I like the solution you are leaning towards.
If you want to hide the comment after three flaggings have been created for it, you need to keep track of who created them, so people can flag just once.
I'd create a flag resource (which can hold any kind of flags your users can assign to particular comment), then a flagging resource which connects flags with comments and holds information about the entity which is responsible for flagging (which could be a user or a user represented by IP).
Every comment will then have many flaggings.
You can use state machine to change the status of a comment to "to_be_revised" or something similar after three flaggings have been added. State machine (aasm_state_machine or the one which is now incorporated directly into Rails) will also provide you with named_scopes for groups of comments with the same state.
After revision, you can set the state to "published" again and delete all the flaggings or to "unpublishable" and so hide it forever.
Perhaps the acts-as-flaggable plugin would work.

How to grab a record ID from a search form in rails

I'm very verrry new to rails.
I'm using the autocomplete (plugin) text field to browse through titles of my records. When the user selects the record, I'd like to forward them to a custom built URL, based on that record's ID.
How should I do this?
Thanks!
-Elliot
To be more specific. I'm trying to make a simple search form, that redirects right to the record filled in by the autocomplete. If the record does not exist, I'd either like a message saying it doesn't yet exist, or a create record page.
UPDATE:
This may be more helpful, how can I just grab the value currently in the text box?
The auto-complete plugin you are using may not be the best for you to use. Here is another option for you to consider:
Model Auto Completer
This plugin returns the text, but also stores the id in a hidden field.
There was another plugin I thought that did this as well, I can't remember it now though.
I think what you are referring to is directing to a page like "/record/my_awesome_product" rather than the boring "/record/1234".
Although this article is a little outdated (in Rails development terms) it still may help you.

How do I update only the properties of my models that have changed in MVC?

I'm developing a webapp that allows the editing of records. There is a possibility that two users could be working on the same screen at a time and I want to minimise the damage done, if they both click save.
If User1 requests the page and then makes changes to the Address, Telephone and Contact Details, but before he clicks Save, User2 requests the same page.
User1 then clicks save and the whole model is updated using TryUpdateModel(), if User2 simply appends some detail to the Notes field, when he saves, the TryUpdateModel() method will overwrite the new details User1 saved, with the old details.
I've considered storing the original values for all the model's properties in a hidden form field, and then writing a custom TryUpdateModel to only update the properties that have changed, but this feels a little too like the Viewstate we've all been more than happy to leave behind by moving to MVC.
Is there a pattern for dealing with this problem that I'm not aware of?
How would you handle it?
Update: In answer to the comments below, I'm using Entity Framework.
Anthony
Unless you have any particular requirements for what happens in this case (e.g. lock the record, which of course requires some functionality to undo the lock in the event that the user decides not to make a change) I'd suggest the normal approach is an optimistic lock:
Each update you perform should check that the record hasn't changed in the meantime.
So:
Put an integer "version" property or a guid / rowversion on the record.
Ensure this is contained in a hidden field in the html and is therefore returned with any submit;
When you perform the update, ensure that the (database) record's version/guid/rowversion still matches the value that was in the hidden field [and add 1 to the "version" integer when you do the update if you've decided to go with that manual approach.]
A similar approach is obviously to use a date/time stamp on the record, but don't do that because, to within the accuracy of your system clock, it's flawed.
[I suggest you'll find fuller explanations of the whole approach elsewhere. Certainly if you were to google for information on NHibernate's Version functionality...]
Locking modification of a page while one user is working on it is an option. This is done in some wiki software like dokuwiki. In that case it will usually use some javascript to free the lock after 5-10 minutes of inactivity so others can update it.
Another option might be storing all revisions in a database so when two users submit, both copies are saved and still exist. From there on, all you'd need to do is merge the two.
You usually don't handle this. If two users happen to edit a document at the same time and commit their updates, one of them wins and the other looses.
Resources lockout can be done with stateful desktop applications, but with web applications any lockout scheme you try to implement may only minimize the damage but not prevent it.
Don't try to write an absolutely perfect and secure application. It's already good as it is. Just use it, probably the situation won't come up at all.
If you use LINQ to SQL as your ORM it can handle the issues around changed values using the conflicts collection. However, essentially I'd agree with Mastermind's comment.

Resources