RailsAdmin custom field for has_many - ruby-on-rails

Using RailsAdmin with this example:
class Product < ActiveRecord::Base
has_many :comments, as: :commentable, inverse_of: :commentable
has_paper_trail
end
class Comment < ActiveRecord::Base
belongs_to :commentable, polymorphic: true, inverse_of: :comments
has_paper_trail
end
When I am viewing a Product, the has_many associated Comments is a comma separated list of the Comment names. I'd like instead to have a table with various attributes the the Comment model in columns.
Further, the default edit view on Product gives me a list of Comments that might be related to other Products, and allows the user to "steal" the Comment and assign it to the current one in view. I guess that might be useful in a Manager/Employee type of association - it would allow a user to switch a Employee from one Manager to another. It's really not what I want though. Instead, I'd like to present a list of existing Comments associated, ability to delete one of these, and ability to add one.
So I'm looking for a starting point on this type of customisation (assuming it's beyond the realm of configuration). I have seem some tips on field customisation but this is something different (isn't it?) I haven't been able to find anything on the web so I'm hoping someone here can get me started...
Thanks,

RailsAdmin does not currently allow for custom partials for the Show action.

Related

Is it possible to have multiple has_many associations with one specific model in Rails?

I have two models with the following associations:
organization.rb
class Organization < ActiveRecord::Base
has_one :user, as: :identifiable
has_many :speakers
#has_many :cast_items
end
speaker.rb
class Speaker < ActiveRecord::Base
has_one :user, as: :identifiable
#has_many :cast_items
end
As you can see, I've commented out an association with the CastItem model.
I want a Speaker to add multiple CastItems. Also, an Organization must be able to add multiple CastItems. When an Organization adds a CastItem, it does not necessarily belongs to a Speaker who is associated with an Organization. In other words an organization must be able to add a CastItem to itself or to a Speaker who is associated with him.
Will it be completely valid to put the has_many :cast_items in both models, or are there more practical design options?
Yes, you can do that. Remember to add organization_id and speaker_id to your cast_items model.
You can check out this link, http://guides.rubyonrails.org/association_basics.html , some useful information regarding many to many and one to many associations.
Personally, in your case, I will use has_many :through
You can definitely do that. I can't think of any reason that would be bad and it's often necessary.
You may want to look up the 'delegate' method for when you're creating CastItems, and have them always created by Organizations.
Also, make sure that if you have a :speaker_id on your CastItem that it can accept nil or false.

What is 'the Rails way' to have forms not directly mapped to an object model?

Let's say I have three tables to accommodate a many-to-many relationship.
class User < ActiveRecord::Base
has_many :user_hobbies
has_many :hobbies, :through => :user_hobbies
end
class UserHobbies < ActiveRecord::Base
belongs_to :user
belongs_to :hobby
end
class Hobby < ActiveRecord::Base
has_many :user_hobbies
has_many :users, :through => :user_hobbies
end
And I wanted to have a form in which a user can input as many hobbies as they want, where each would be stored into the correct tables (in my situation, the 'Hobby' table is preset, the user may select from enumerated values, not add them)
How would I go about producing a form to achieve this? I would use JavaScript for Auto-Completion and dynamic fields (i.e. each time you enter a field, another appears).
In order to produce this form there are two options which are available to you..
a. The first option is to use the virtual attribute which would accept the comma separated values of the hobbies of the user and then when you save the user the virtual attribute would then set the hobbies corresponding to the respective user..See this railcast
http://railscasts.com/episodes/16-virtual-attributes
http://railscasts.com/episodes/167-more-on-virtual-attributes
b. The second option is to use the accepts_nested_attributes_for which would automatically include the attributes for the nested relationships in the parent model...Consult the API for more information on accepts_nested_attributes_for
Also I can see a bug in the above definitions as there should be has_many :users :through :user_hobbies relationship in the Hobbies table becoz we might want to know the user having the..
As far as the auto-completion is concerned, there is a gem called as auto-complete-rails and its documentation is pretty simple and standard to use and understand..

Rails checkboxes for a has_many :through association

A person can compete in various events, but they must enter a partner's name for that event. This association is stored in an entry, which contains a field for the partner's name.
class Person < ActiveRecord::Base
has_many :entries
has_many :events, :through => :entries
validates_presence_of :name
end
class Event < ActiveRecord::Base
end
class Entry < ActiveRecord::Base
belongs_to :person
belongs_to :event
validates_presence_of :partner_name
end
The question is: How do you create a single page form that allows a person to enter themselves in multiple events and input their partners' names? I've tried to implement an all_entries method in the person model that will return an array of entry objects for all the available events, and an all_entries_attributes method that will update, create, and delete entry objects, but I can't seem to find a good, clean way to do this. I know this is a rather open ended question, but this must be a pattern that someone else in the rails community has encountered before, so I'm hoping there is a good solution to it.
If you are still looking for the answer you might want to check out my question and answer that I found.
So you want a page in which you can create events for a user, and add people to those events
I won't give you a plain solution because there's some work to an implementation for this, but I will recommend checking out these railscasts about nested model forms
part1 and part2

How to associate descendant models to a parent

I have the following models:
Account
has_many :libraries
Library
has_many :topics
belongs_to :account
Topic
has_many :functions
belongs_to :library
Function
has_one :example
belongs_to :topic
Example
belongs_to :function
I would like to be able to able to do things such as:
some_account.libraries
some_account.topics
some_account.functions
some_account.examples
In addition, I would like to be able to assign an account to a descendant, i.e
some_example.account = some_account
some_function.account = some_account
some_topic.account = some_account
some_library.account = some_account
To give some context:
I am letting a user (Account) create each Library, Topic, Function, Example. record separately. Then a user is free to change how the records are associated: Change the topic of a Function, move a Topic to a different Library, add an example to a function, and so on.
To my understanding no matter what record is created, I would need to assign it to a user (account) so that I can have a list of each Model records that a user has created, as well as prevent other users from seeing stuff that doesn't belong to them
Although I might be overcomplicating, I really don't know :(
Thanks in advance.
Just put
belongs_to :account
on each entity a user can make... and add a foreign key, and
Account
has_many :libraries
has_many :topics
has_many :functions
has_many :examples
(Note: I use the hobo_fields gem to make migrations easier)
That way.. if they change which functions are in which topics etc.. you can't loose who created it.
If you want to make sure users cannot add their topics to someone else's library just put validation on the record to prevent it.

Help with rails content filtering

Im creating my own blog managing app in rails (for experimental purposes).... What would the best way to get this done?
I have posts and categories.
I want to have a dropdown of categories for the user to select one when they create a new post.
Now, each user will have different privileges so not all categories should appear for all users....
Right now Im at the point where I can create posts and choose which category I want... I havent added any filter per user support....
please help me on where should I go now??
First you will need to implement authentication and authorization. There are many good Rails tutorials on these subjects so I won't go into more detail here.
At this point you will have models for User, Post, and Category. You need a list per-user of authorized categories. A naive model:
class User < ActiveRecord::Base
has_and_belongs_to_many :categories
end
But that is misleading, because a user isn't actually "in" any categories. You probably want something like a join model like so:
class UserCategoryAuthorization < ActiveRecord::Base
belongs_to :user
belongs_to :category
// More fields here; possibly:
// belongs_to :authorized_by, :class_name => 'User'
end
class User < ActiveRecord::Base
has_many :user_category_authorizations
has_many :authorized_categories,
:through => :user_category_authorizations,
:source => :category
end
To start with I would give Users a has_many categories relationship(Which you could turn into its own model object at some point if this idea gets more complicated..or now if it already makes sense) and then assuming you already have log in functionality you can ask the logged in user for its categories and populate the drop down appropriately.
If this is a security issue rather than just convenience then you will need to validate the chosen category is in the users categories when the form is submitted back to the server.
If you don't already have logins I believe there are several rails plug-ins that attempt to help you get this functionality quickly.

Resources