Kevin Brown
Kevin Brown

Reputation: 12650

Rails modeling admin-controlled fields

I have 10 drop-down fields in a "post" form that I want to be controlled by the admin user.

Currently, I have another model called "post fields"

class PostField
  include Mongoid::Document
  field :family, type: String
  field :project, type: String
  field :event, type: String
  field :testmode, type: String
  field :location, type: String
end

I then use these documents to populate each dropdown for a new post.

I have 2 dilemmas:

  1. If the admin adds a PostField document for one dropdown, it creates a whole document. Not the most efficient thing, but I can deal with that.
  2. If the admin changes the name of a field, all associated posts won't match.

This is effectively a tagging model, but poorly implemented b/c I'm not sure how to do it.

ie, I want to have a controlled list of locations: ["Upstairs", "Downstairs", "Bathroom"] so users can't just use a text input and say "Restroom" instead. But if an admin wants to change "Bathroom" to "Restroom", I want the posts to update accordingly.

Hope this isn't too complicated.

Upvotes: 4

Views: 108

Answers (2)

Code-Source
Code-Source

Reputation: 2243

Not sure to anderstand what you want to do.

With this schema, you should be able to create a "post" with static fields and dynamic fields.

simple schema

PostField

  • The "sorting" field is used to sort post's field with respect to each other.
  • The "code" field will contain the code to identify post's field. Once created, it should be static.
  • If you want to make it dynamic, you should add a field "label" so administrator can add new post's field

PostFieldValue

  • The "sorting" field is used to sort values with respect to each other.
  • The "label" field is in your example ["Upstairs", "Downstairs", "Bathroom"]

In model

You should create a method to get the value based on post's field.

Improvements

  • You can improve the schema by adding a "isdefault" field to values so administrator can force the default value in drop-down.
  • You can categorize post's field if needed

Upvotes: 0

Chloe
Chloe

Reputation: 26274

So you want each PostField to be of a certain type, or location? So you want to limit the values that go into :location?

What you have to do is create another model and table called locations, and that will contain a list of locations like Upstairs, Downstairs, Bathroom, etc. It has and id, and a name. In your PostField, you will join to the Locations with has_one :location, and the database table will have location_id. Then the admin can change Location.name to anything they want, while all the PostFields will show the updated label. In the view, use postField.location.name to display the label for that field.

Upvotes: 3

Related Questions