How to design a system like google base with dynamic attributes? in Ruby on Rails - ruby-on-rails

I am wanting to create an application that can allow users to add products for sale.
I want to make it so that a user can add whatever type of product he/she likes and let them also create stored and searchable attributes for their products - alot like google base does.
Does anyone know of the best way to do this ie model it.
I don't really want a table for each category as this would be possibly 1000s of tables.
What is the best way to do this? has anyone got good / bad experiences of this?
Is there any plugins that does this?
Any help would be great
thanks
rick

It sounds like what you want is a tag system.
If you want something more flexible you might want to look at using a document store instead of a database, for example CouchDB.

If you don't want to keep this in a relational database I'd suggest creating a Model called "Descriptor" that would contain the ID of the item being added, the name of the attribute "Color" and the value "Red".
To help keeps things consistent you could also structure pre-set groups of descriptors (for cars: make, model, color) as well as provide auto-completes for the value entry text fields.

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.

Ruby on Rails: How to have multiple controllers for one table AND multiple models

I'm new to Ruby and to Rails. I have played a bit with Sinatra but I think that Rails is a more complete framework for my project. However, I am running into trouble with this.
I am working with an fairly substantial existing, and heavily used, mySQL database and I am trying to build an API for this that will report on certain features. The features that are needed are, for the most part, counts of records by certain groupings, then drilling down into details.
For example we have a table - tableA, that contains lots of information relating to documentation. One piece of information we want to report on from that is the number of items in a given language. The language code is stored against each item and based on a get request I would like to return JSON.
Request: /languages/:code/count/:tablename
There are two variables in that most specific URL - the code we are counting and the table we are counting from.
I understand that in routes.rb I can set up a mapping:
get '/languages/:code/count/:table', :controller=>'languages', :action=>'count'
I have a controller - languages_controller.rb with a count method in it. this then matches to a corresponding view file count.html.erb
In all the tutorials I have read and examples I have followed the main point seems that 'languages' would be a table in the database and would therefore be available under the 'magic' Rails approach.
My issue is that it is not a table, rather the results of the call should be a limited subset of the fields in tableA. Such as languagecode and count(id).
The description of the language needs to be looked up 'manually' as it is stored as an internal code that is not in a database anywhere (historic decision/madness).
The questions:
how do I have a model that is only a subset of fields, plus some that are manually populated - languagecode, isocode, description, count
Am I right in thinking that once I have the model defined as such as I could use ActiveRecord to get data from the database and then in the controller add the extra information in?
Can I change table in the model based on the parameter sent in the URL?
Essentially, I am at a loss at the moment on what to do with this. I have the routes defined, the view templates in place and the controller there and ready to go. The database component - getting some data from a pre-existing table seems mysterious to me.
Any help is greatly appreciated, it seems that the framework is currently getting in my way and I know that I can't be the only one trying this sort of thing so if you have any advice please share.
There's really no need for a model here, at all. This isn't what ORMs are for. What you should be doing is just running raw SQL against the database, and iterating over the results. Consider doing something like this: https://stackoverflow.com/a/14840547/229044

Adding custom attributes to Task?

How can i add custom attributes/data to Task via API . for example we wanted to add field like customer contact number or deal amount e.t.c
We don't currently support adding arbitrary metadata to tasks, though it's something we're thinking about. In the meantime, what many customers do is to simply put data in the note field in an easily-parseable form, which works well and also lets humans reading the task see the e.g. ticket number.
It's not a terribly elegant solution, but it works.
https://asana.com/developers/documentation/getting-started/custom-external_data
Custom external data allows a client application to add app-specific metadata to Tasks in the API. The custom data includes a string id that can be used to retrieve objects and a data blob that can store character strings.
See the external field at https://asana.com/developers/api-reference/tasks

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 structure content in Ruby (RoR) app?

I am in the process of building a super simple CMS to handle smaller "static" page type projects (e.g. - small sites for friends). I have different "page types" that I would like to add. I built something similar in Coldfusion previously. Looked something like this:
table content_type:
content_type_code varchar(10)
content_type_name
table content:
content_id
content_type_code varchar(10)
content_name
content_desc
content_url
I would create a content type called "blog" or "photo" and each time a content was added, it'd get assigned the content_type_code. Then in /blog/ I'd query for all content that had a content_type_code of "blog".
Now that I'm using Ruby/RoR I am trying to think about things differently. I was thinking a better way might be to use nested pages with awesome_nested_set (https://github.com/collectiveidea/awesome_nested_set). But I'm not sure if that's the best solution.
Then I could create a page called "blog" and add to that many pages. So essentially the top level would take place of the "content_type" from my previous example.
Can someone steer me in the right direction on what the best method would be? I'm a newb looking for a kick in the right direction.
EDIT
I should add that the only real thing that I would change between the different "types" of content would be the layout and where they are displayed ("photo" content at /photos/, "blog" content at /blog/).
I try to recap:
You want to build a CMS
Your CMS manages a single web site
A web site is made of content
There are differenti type of contents, and I am assuming every type of content has its own behaviour
Contents are organized in a tree
Here is the plan I suggest you:
Create the Content resource; use the scaffold to have something already working, adding few field (title and body in example)
Add validations to your new model
Write a couple of unit tests against your validation (quite useless, just to see how it works)
Install awesome_nested_set and manage to make it working with your model
Work on the UI to make it quite easy to create new content, move content around, edit a single content
Now its time to implement the content types; STI is the Rails way, but I have to warn you it can be really hard. I suggest you to reiterate 1, 2, 3, 5 to create new models for Photo and BlogPost
Once you will be there, you will have hundreds of ideas to implement. Have fun :)
Instead of using content_type I would rather let the user choose a model on a selection page, like "photo" or "blog" and load an editing page based on that selection. So the user wants a new 'blog'-entry they get redirected to blog/new or 'photo' for photo/new. It's the easiest way to go in terms of usability and your controlling backend and you don't have redundant data (for example an empty photo url which is not necessary in a blog-type) in your database.

Resources