Overriding base address fields of res.partner with custom functionality - field

I am working on customization of openerp which is UK specific so I want to override fields of res.partner like zip and state_id. I want to use post codes instead of zip and County instead of state. There are also some apps which depend on these fields so I don't want to add new ones. The zip should remain the same but instead of zip I want it to use post code lookup for which I have the model. The state_id field should also be used as county.
The postcode model has the following fields (Data is not real)
_________________________________________________________________________
| PostCode_id | Postcode | longitude | Longitude | Town | County |
-------------------------------------------------------------------------
| 1 | 'AS34' | 53454 | 435345 | Aberdeen | Aberdeen |
.........................................................................
| ....
I want the zip to be a many2one field, when the user type the postcode it should offer auto completion. The base zip field in res.partner is of type 'char'. The state_id is a many2one field but I want to connect it to county column.

I got it. Basically setting postcode column as _rec_name solved the issue. It is also a good idea to make another table or model for county and to add foreign key in this table. This way I was able to achieve what I wanted.
Thanks anyways

Related

Rails using Views instead of Tables

I need to create a Rails app that will show/utilize our current CRM system data. The thing is - I could just take Rails and use current DB as backend, but the table names and column names are the exact opposite Rails use.
Table names:
+-------------+----------------+--------------+
| Resource | Expected table | Actual table |
+-------------+----------------+--------------+
| Invoice | invoices | Invoice |
| InvoiceItem | invoice_items | InvItem |
+-------------+----------------+--------------+
Column names:
+-------------+-----------------+---------------+
| Property | Expected column | Actual column |
+-------------+-----------------+---------------+
| ID | id | IniId |
| Invoice ID | invoice_id | IniInvId |
+-------------+-----------------+---------------+
I figured I could use Views to:
Normalize all table names
Normalize all column names
Make it possible to not use column aliases
Make it possible to use scaffolding
But there's a big but:
Doing it on a database level, Rails will probably not be able to build SQL properly
App will probably be read-only, unless I don't use Views and create a different DB instead and sync them eventually
Those disadvantages are probably even worse when you compare it to just plain aliasing.
And so I ask - is Rails able to somehow transparently know the id column is in fact id, but is InvId in the database and vice versa? I'm talking about complete abstraction - simple aliases just don't cut it when using joins etc. as you still need to use the actual DB name.

rails user-defined custom columns

I am using Ruby on Rails 4 and MySQL. I have three types. One is Biology, one is Chemistry, and another is Physics. Each type has unique fields. So I created three tables in database, each with unique column names. However, the unique column names may not be known before hand. It will be required for the user to create the column names associated with each type. I don't want to create a serialized hash, because that can become messy. I notice some other systems enable users to create user-defined columns named like column1, column2, etc.
How can I achieve these custom columns in Ruby on Rails and MySQL and still maintain all the ActiveRecord capabilities, e.g. validation, etc?
Well you don't have much options, your best solution is using NO SQL database (at least for those classes).
Lets see how can you work around using SQL. You can have a base Course model with a has_many :attributes association. In which a attribute is just a combination of a key and a value.
# attributes table
| id | key | value |
| 10 | "column1" | "value" |
| 11 | "column1" | "value" |
| 12 | "column1" | "value" |
Its going to be difficult to determin datatypes and queries covering multiple attributes at the same time.

How to display TIMEDIFF(now, then) in a DB Grid?

Sorry, I am very new to DbGrids.
Should I use the query's field editor and somehow add a new field that captures the TIMEDIFF and then just add that as a column in my DbGrid?
Or can/should I skip the field editor and somehow declare the TIMEDIFFF as a column?
For this table, I want a DbGrid with 4 columns : start time, end time, duration, description (run_id is the primary key & will not be displayed).
I am stumped as to how to get data into a 'duration' column ...
mysql> describe test_runs;
+------------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+-------------+------+-----+---------+----------------+
| run_id | int(11) | NO | PRI | NULL | auto_increment |
| start_time_stamp | timestamp | YES | | NULL | |
| end_time_stamp | timestamp | YES | | NULL | |
| description | varchar(64) | YES | | NULL | |
+------------------+-------------+------+-----+---------+----------------+
4 rows in set (0.37 sec)
[Update]
The query for the datasource is
SELECT start_time_stamp,
end_time_stamp,
TIMEDIFF(end_time_stamp, start_time_stamp) as duration,
description
FROM test_runs ORDER BY start_time_stamp DESC
and when I execute it manually in MySql, I get
mysql> select TIMEDIFF(end_time_stamp, start_time_stamp) as duration FROM
+----------+
| duration |
+----------+
| NULL |
| 00:04:43 |
| 00:00:13 |
| 00:00:06 |
| 00:00:04 |
+----------+
5 rows in set (0.00 sec)
but the corresponding column in the DB grid remains blank. Can anyone help? Thanks.
[Update] I am using AnyDac, if that helps. The query produces all fields, including the time difference, in MySql and also in the Delphi IDE when I use the AnYDac query editor and execute it.
The only problem is that I don't see it in the DB grid at run time. I double click the DB grid at design time and the columns are correct. The FielName property is set to duration, which is retuned by the query shown above. It doesn't exist in the database, but is calculated by the query; could that somehow be the problem??
[Aaaaaaaargh!!!] Someone tried to "improve" my code and set the query's text programatically at run-time (as SELECT * FROM test_runs), thus overwriting my design time query!! Since the databse table does not have a duration field, none was shown in the DB grid.
Words were had, voices were rasied and now I must apolgize for wasting your time. Sorry.
I would create a calculated field in your Query and add that field to your DbGrid.
so, as you say, with the Field Editor open for the query, right-click and select New Field (or press Ctrl-N). Give your new field a name (eg Duration), keep Component name default or rename if you desire. Set the Type as appropriate (DateTime most likely in this case) and set the Field Type to Calculated.
Then, in the OnCalcFields event of your Query, set the value of that field to the value you want. eg:
procedure TForm1.Query1CalcFields(DataSet: TDataSet);
begin
Dataset.FieldByName('description').AsDateTime :=
DataSet.FieldByName('end_time_stamp').AsDateTime -
DataSet.FieldByName('start_time_stamp').AsDateTime;
end;
Or, you could also include the Duration as an extra field in your select query. Unfortunately I don't have ready access to MySQL here, but it could be something like:
select run_id, start_time_stamp, end_time_stamp, description,
(end_time_stamp - start_time_stamp) as duration from test_runs;
Should I use the query's field editor and somehow add a new field that
captures the TIMEDIFF and then just add that as a column in my DbGrid?
Not at first, delete all of the entries from that field editor
Or can/should I skip the field editor and somehow declare the
TIMEDIFFF as a column?
Not at first, delete all of the entries from that column editor
After that you should see all columns from the open dataset inside the grid, because you eliminate all the limitations from column editor and field editor.
As a guess what happens to your grid/data you did not set the FieldName property for column duration and therefor the grid didn't know what to present.
to use a DBgrid you need a datasource,dataset each should be assigned to respective components,and make sure you have made the connection. if you didnt get any thing in the grid you need to check your dataset,if only the date difference column data is missing but header display the field name in the query then probably you may be getting null data.....if even you didn't get the header name too you need to check your query.......

Dynamic categories criteria with matching products rails 3

I'm working on a new rails project, and in this project I have products and product categories.
So these categories are very different from each other, to name some, Boats, Houses, Cars.
The car category might have criterias like "Mph", "Model", "Brand", "Year" and so on. Where the house category will have something like "Rooms", "Year", "City", "Postal Code" etc.
I would like this to be very dynamic, so that i would be able to add/remove criterias and add/remove categories from a backend panel.
Now to my question, i have been playing around with this, and i can't really figure out the logic of this concept, i have tried some solutions, however they are very weird and quite inefficient. Maybe some hardcore rails coder could give me a hint, on how to solve this puzzle?
So the best solution i could come up with, was this:
Four models :
_______________________
| Product.rb |
-----------------------
| id | integer |
-----------------------
| category_id | integer |
-----------------------
| Title | string |
-----------------------
| Description | text |
-----------------------
_______________________
| Category.rb |
-----------------------
| id | integer |
-----------------------
| Title | string |
-----------------------
| Description | text |
-----------------------
_______________________
| Criteria.rb |
-----------------------
| id | integer |
-----------------------
| category_id | integer |
-----------------------
| Name | string |
-----------------------
| Default | string |
-----------------------
| Description | text |
-----------------------
_______________________
| ProductInfo.rb |
-----------------------
| id | integer |
-----------------------
| product_id | integer |
-----------------------
| Name | string |
-----------------------
| Value | text |
-----------------------
How it's connected :
Criteria.rb is connected to Category.rb with a category_id and has_many/belongs_to relation
Product.rb is connected to Category.rb with a category_id and has_many/belongs_to relation
ProductInfo.rb is connected to Product.rb with a product_id and has_many/belongs_to relation.
Category.rb is the heart og this solution. The category model, both have many products and criterias.
How it should work, in reality :
In the show category page, i would first print out all the criterias for the given category.
Afterwards i would make a #products.each do |product|.
In the #products.each block, i would make a #category.criterias.each do |criteria|.
In the #category.criterias.each block, i would then run something like product.productinfos.where(:name => criteria.name).
And then run it one by one.
Conclusion, this solution do work, however i doubt that it is the best solution. It will make an extremely big loadtime, with high traffic and many data. And i will need to write very weird and unreadable code.
This is a rather long question, and it might be very confusing, so if there is anything please just say. Also, i have searched quite alot for a question like this, both on Stackoverflow, and on google but i have not been able to find anything like this.
Oluf Nielsen.
In my opinion, it's better not to define additional tables to handle this due lots of performance issues. My preference to handle such things is to use a serialized column in the products table. Ability to search directly in the database is reduced with this approach, but then you wouldn't want to do that anyway. To handle search, you have to add some sort of indexed searching mechanism. Like acts_as_ferret or even Solr or ElasticSearch.
If you are using postgres check out https://github.com/softa/activerecord-postgres-hstore
For Mysql, use the rails's built in 'store'
http://api.rubyonrails.org/classes/ActiveRecord/Store.html
class Product < ActiveRecord::Base
has_and_belongs_to_many :categories
store :settings
end
To set criteria for each category do something similar to this:
class Category < ActiveRecord::Base
has_and_belongs_to_many :products
def criteria
#criteria_list ||= self[:criteria].split('|')
#criteria_list
end
def criteria=(names)
self[:criteria] = names.join('|')
end
end
Everytime a product is added to a category, check if all of the criteria in that category is available in the product's properties hash keys. If not, add it with a default value if needed.
You can also setup accessors for the properties hash store using a proc that dynamically gets the accessor names from the all the criteria field of the categories of the product? (not sure about this, cause I haven't done this before)
You can also look into using STI (Single table Inheritance) using a type field in your products table. (It's well documented) This approach is slightly better 'cause when products move from one category to another, the properties won't change.
class Gadget < Product
store_accessor :manufacturer, :model
end
class Phone < Gadget
store_accessor :os, :touch_screen, :is_smart
end
Hope this helps
Or else, second approach would be go with a nosql database. Try mogodb with mongoid, which is quite stable. This will suite your requirements for variable attributes very well. Also you can add any other categories later with very ease.
As far as I can see, with mysql, you will end up creating multiple dbs for storing this dynamics data and that is bound hamper the performance.
UPDATE -
Apart from the point that your data can be flexible with nosql, there are many things you need to consider before shifting there. I just suggested based on fact that you need flexible database structure. Start with mogodb docs, they are good starting point.

Cassandra cql kind of multiget

i want to make a query for two column families at once... I'm using the cassandra-cql gem for rails and my column families are:
users
following
followers
user_count
message_count
messages
Now i want to get all messages from the people a user is following. Is there a kind of multiget with cassandra-cql or is there any other possibility by changing the datamodel to get this kind of data?
I would call your current data model a traditional entity/relational design. This would make sense to use with an SQL database. When you have a relational database you rely on joins to build your views that span multiple entities.
Cassandra does not have any ability to perform joins. So instead of modeling your data based on your entities and relations, you should model it based on how you intend to query it. For your example of 'all messages from the people a user is following' you might have a column family where the rowkey is the userid and the columns are all the messages from the people that user follows (where the column name is a timestamp+userid and the value is the message):
RowKey Columns
-------------------------------------------------------------------
| | TimeStamp0:UserA | TimeStamp1:UserB | TimeStamp2:UserA |
| UserID |------------------|------------------|------------------|
| | Message | Message | Message |
-------------------------------------------------------------------
You would probably also want a column family with all the messages a specific user has written (I'm assuming that the message is broadcast to all users instead of being addressed to one particular user):
RowKey Columns
--------------------------------------------------------
| | TimeStamp0 | TimeStamp1 | TimeStamp2 |
| UserID |------------|------------|-------------------|
| | Message | Message | Message |
--------------------------------------------------------
Now when you create a new message you will need to insert it multiple places. But when you need to list all messages from people a user is following you only need to fetch from one row (which is fast).
Obviously if you support updating or deleting messages you will need to do that everywhere that there is a copy of the message. You will also need to consider what should happen when a user follows or unfollows someone. There are multiple solutions to this problem and your solution will depend on how you want your application to behave.

Resources