Is there a Rails way of making a form with a nested polymorphic relation (new or existing)? - ruby-on-rails

I have a model Navigation that has a polymorphic target. That target can be a Page, an ExternalLink, or a Plugin. Each of these targets has different attributes (page has content, title, external link has title, url, plugin has its own polymorphic relations.
I'm building a little form to add a Navigation link, and I want to give my users the option to select an existing one of these relations (which is easy enough, just a grouped collection select), but I also would like to let them select a 'New' option for each of these target models.
The question is -- how do I include it in the same form?
I could make a form object, but that feels like the last-resort option. I know I can accept nested attributes for target, but I don't know how to do that with a selection form as well. I know I could do some js hackery to add or remove form elements on selecting or not selecting the 'new' options.
All of these don't feel railsey. This problem feels like a not-uncommon issue, and I imagine this is a solved problem that I'm trying to reinvent the wheel for. What option am I missing? What is the rails way to make this?

You may want to look at the Cocoon gem, will handle a lot of the "js hackery" you alluded to.
This post shows how to use Cocoon to render the appropriate fields for (in your case) each of the possible target classes.
Whether you have a separate button for each target class is up to you.

I think you should use globalid gem for this.
Here is a good article describing how it works.
GlobalID helps you uniquely identify your models. E.g. gid://Company/1 and gid://Employee/1 has the same IDs, but different GlobalIDs. You can actually find model simply by using GlobalID::Locator:
GlobalID::Locator.locate gid_value
This should make simple combo-boxes easily programmable.

Related

End user can edit the view of a recored in some sort of editor - rails

I'm looking for something like an editor but instead of the user editing just a post or page the user can edit the default template layout or view layout for all pages associated to something.
I'll give an example to get my question across because i really don't know what it would be called.
Say for example you have categories. (people, animals, etc..)
Now imagine there is a category of people, each person in the category has the keys 'height', 'weight' and 'age' and each key has values assigned to them (height: 120 cm, weight: 80 pounds, age: 25)
I want the user to be able to edit the layout of the people page (page template or view that each person is displayed on) and be able to add in the keys they want to be displayed on the page.
So say they add the keys ‘height’ and ‘weight’ it will only display the height and weight on every person page, plus whatever pre set text the user added in the template editor for the people pages to display.
I could probably find a good editor that can be customised and change it to my liking but there might already be gems out there made for this.
My question is, is there a gem for basically letting users set templates for record pages.
Even if there is something out there to change views for objects but the user cant set the keys on it, i'd still like to see it because I might be able to add the key/value functionality to it.
Hopefully you understand what I'm looking for.
UPDATE
Ok so i appears I'm after an editor that works with a template engine like liquid or one thats both an editor and template engine.
If you plan on building from scratch, it sounds like it could be done using rails generate scaffold this allows views/models/controllers to create pages for each of your categories.
Then you can use Associations to create associations between your categories.
You can also make different user roles by making a field called "Role" in your Users relation
Otherwise, there are also open-source software Active Admin to manage content for your categories.
EDIT: After looking around I found a gem you can use to have end-users edit your own views, feel free to check out Liquid.

How can I make a rails model searchable by the user?

I'm trying to expose a search feature in rails. I want a user to be able to enter a string like name:"john" color:"blue" and get a list of ActiveRecord objects for some model that have a name attribute containing john and a color attribute containing blue. I'd also like them to be able to use and and or and parentheses e.g. name:"john" or color:"blue" or (name:"john" color:"blue") or name:"bill". Ideally they could also use things like age<20 where age is an numeric field. Is there a rails plugin that does this. I've was looking briefly at sphinx and ferret both of which seem to create an api for this but it was unclear whether they provided a clear text based option or if I would need to parse the search strings myself.
Ernie's Ransack gem is a good place to start.
You will have to provide an intermediate layer between your submitted form and the Ransack code (this would be a good idea anyway for security reasons) to convert strings from the format you desire to something Ransack can understand.
If you check the demo page and the documentation for the gem you'll find it's quite simple to create the sort of queries you're after.
Watch how GET requests are generated from the conditions you build and in your application replace the builder Ernie has in the demo with a single textfield accepting strings like (name:"john" color:"blue") or name:"bill". Do some pattern matching when this field is submitted and build a proper querystring to pass onto the Ransack gem.
Edit
For future questions like "what's a popular gem for ______?", check out The Ruby Toolbox. If Ransack doesn't suit your needs, perhaps a gem in the Rails Search category has what you're looking for. I personally use Ransack for exactly what you're describing; providing a custom query interface for my application's User model.
I'd suggest doing your own search class. I find that for each app I do, the needs of search change considerably and it's simple enough to create a search app that considers all the variables you might want in a search query, posed against any number of classes you want to search.
In your Search class, have it return a collection, in the order you desire, and the collection can be made up of object instances that the searcher may desire.

How to create a tagging system like on Stack Overflow or Quora

I want to create a tagging system like seen here on Stack Overflow or on Quora. It'll be its own model, and I'm planning on using this autocomplete plugin to help users find tags. I have a couple of questions:
I want tags to be entirely user-generated. If a user inputs a new tag by typing it and pressing an "Add" button, then that tag is added to the db, but if a user types in an existing tag, then it uses that one. I'm thinking of using code like this:
def create
#video.tags = find_or_create_by_name(#video.tags.name)
end
Am I on the right track?
I'd like to implement something like on Stack Overflow or Quora such that when you click a tag from the suggested list or click an "Add" button, that tag gets added right above the text field with ajax. How would I go about implementing something like that?
I know this is kind of an open-ended question. I'm not really looking for the exact code as much as a general nudge in the right direction. Of course, code examples wouldn't hurt :)
Note I am NOT asking for help on how to set up the jQuery autocomplete plugin... I know how to do that. Rather, it seems like I'll have to modify the code in the plugin so that instead of the tags being added inside the text field, they are added above the text field. I'd appreciate any direction with this.
mbleigh's acts_as_taggable_on gem is a feature-complete solution that you should definitely look into a little more closely. The implementation is rock-solid and flexible to use. However, it is mostly concerned with attaching tags to objects, retrieving tags on objects, and searching for tagged items. This is all backend server stuff.
Most of the functionality you are looking to change (based on your comments) is actually related more to your front-end UI implementation, and the gem doesn't really do much for you there. I'll take your requests one-by-one.
If user inputs a new tag, that tag
gets added, if user inputs an
existing tag, the existing tag gets
used. acts_as_taggable_on does this.
Click a tag from suggested list to
add that tag. This is an
implementation issue - on the
back-end you'll need to collect the
suggested list of tags, then display
those in your presentation as links
to your processing function.
Autocomplete as user enters
potential tag. You'll use the jQuery
autocomplete plugin against a list
of items pulled off the tags table.
With additional jQuery, you can
capture when they've selected one of
the options, or completed entering
their new tag, and then call the
processing function.
Restrict users to entering only one
tag. This will be your UI
implementation - once they've
entered or selected a tag, you
process it. If they enter two words
separated by a comma, then before or
during processing you have to either
treat it as one tag, or take only
the text up to the first comma and
discard the rest.
When you process the addition of a
tag, you will have to do two things.
First, you'll need to handle the UI
display changes to reflect that a
tag has been entered/chosen. This
includes placing the tag in the
"seleted" area, removing it from the
"available" display, updating any
counters, etc. Second, you'll need
to send a request to the server to
actually add the tag to the object
and persist that fact to the
database (where the taggable gem will take over for you). You can either do this via
an individual AJAX request per tag,
or you can handle it when you submit
the form. If the latter, you'll need
a var to keep the running list of
tags that have been added/removed
and you'll need code to handle
adding/removing values to that var.
For an example of saving tags while editing but not sending to server/db until saving a form, you might take a look at the tagging functionality on Tumblr's new post page. You can add/remove tags at will while creating the post, but none of it goes to the database until you click save.
As you can see, most of this is on you to determine and code, but has very little to do with the backend part. The gem will take care of that for you quite nicely.
I hope this helps get you moving in the right direction.
The more I try to force the acts-as-taggable-on gem to work the more I think these are fundamentally different types of problems. Specifically because of aliases. The gem considers each tag to be its own special snowflake, making it difficult to create synonyms. In some cases it doesn't go far enough, if you want the Tag to have a description you'd need to edit the given migrations (which isn't hard to do).
Here's what I'm considering implementing, given the trouble I've had implementing via the gem. Let's assume you want to create a tagging system for Technologies.
Consider the following psuedo code, I haven't yet tested it.
rails g model Tech usage_count::integer description:text icon_url:string etc. Run the migration. Note the
Now in the controller you will need to increment usage_count each time something happens, the user submits a new question tagged with given text.
rails g model Name::Tech belongs_to:Tech name:string
Name::Tech model
belongs_to :tech
end
Then you could search via something like:
search = Name::Tech.where("name LIKE :prefix", prefix: "word_start%")
.joins(:tech)
.order(usage_count: desc)
.limit(5)
This is starting point. It's fundamentally different from the gem, as each tag is just a string on its own, but references a richer data table on the back end. I'll work on implementing and come back to update with a better solution.

Parsing blog post tags from a text_field

Ok, so you know how you ask a question here, and in the "Tags" field you can enter several space-separated tags into a single text field?
I'm trying to replicate similar behavior in my Rails app. Except instead of questions, I'm doing a blog app (which has "posts"), and tagging those.
I'm using "form_for" to build the quick form. Inside of that I have the line:
f.text_field :tags
The problem I'm running into is, "tags" is not a field on my Post class. My Post class HABTM tags. So, somehow I need to parse the tags text field (using String.split), and pass the resulting tag Strings into my controller, so my controller can create and associated the tags along with the new blog post.
Is using "form_for" not going to work in this case? Is doing this sort of behavior beyond the design of the quick-and-dirty "form_for" functionality?
Thanks!
Unless you really want to reinvent the wheel, I would suggest using a plugin for this. ActsAsTaggableOnSteroids is a mature one. http://svn.viney.net.nz/things/rails/plugins/acts_as_taggable_on_steroids
Agree with Ben on this - there's lots of great plugins and features/helpers that make them simple to use. And you can learn a lot about how to do this in a well-designed way. Here's another good choice.
http://github.com/mbleigh/acts-as-taggable-on

Best way to implement simple sorting / searching in Rails

What's the best way to implement an interface that looks like this in rails?
Currently I'm using Searchlogic, and it's a tad painful. Problems include:
Making sure certain operations remain orthogonal -- for example, if you select "Short Posts" and then search, your search results should be restricted to short posts.
Making sure the correct link gets the "selected" class. Right now the links are <a>'s, so maintaining this state client-side is tricky. I'm hacking it by having the AJAX response to, say, sorting return a new sort links section with the correct link "selected". Using radio buttons instead of <a> tags would make it easier to maintain state client-side -- maybe I should do that?
I recently solved a similar problem using named_scopes and some ruby metaprogramming that I rolled up into a plugin called find_by_filter.
find_by_filter accepts a hash of scope
names and values, and chains them into
parametised scope calls. If the model has a named_scope that matches the provided name, this is called.If no named_scope is found, an anonymous scope is created.

Resources