Simple Form Configuration - simple-form

In simple_form.rb how do I set time_zone_priority and country_priority. The following does not work.
config.time_zone_priority = ["IN", "US", "CA", "UK"]
config.country_priority = ['India', 'United States']
In the view file, however, the following works:
= f.country_select :country_code, priority: %w(IN US CA UK)
Please assist

I had this problem too just now. Then, in the initializer, when I tried the country codes instead of the full country names, it worked.
config.country_priority = ['IN', 'US', 'CA', 'UK']

Related

Get meta information from the database with Eloquent

I'm probably missing something simple here but how do I get the primary key for a table using Laravel/Eloquent? When I execute the following I'm getting an Incorrect Syntax error while it works when executing directly on the database.
$primarykey = DB::table('table')->whereRaw('SHOW KEYS FROM table WHERE Key_name = "PRIMARY"')->get();
I tried it with following code and it worked.
$primarykey = DB::select('SHOW KEYS FROM users WHERE Key_name = "PRIMARY"');
I had used select of DB without get method because it returns array like this
[
{#3558
+"Table": "users",
+"Non_unique": 0,
+"Key_name": "PRIMARY",
+"Seq_in_index": 1,
+"Column_name": "id",
+"Collation": "A",
+"Cardinality": 1,
+"Sub_part": null,
+"Packed": null,
+"Null": "",
+"Index_type": "BTREE",
+"Comment": "",
+"Index_comment": "",
},
]
So please try and let me know if you find any issue in it.
Thanks

Sort by datetime, then group by date

I want to sort by a datetime value, and group by a date string.
I have found the sortProperty on the grouper:
You can set this configuration if you want the groups to be sorted on something other then the group string returned by the groupFn. This serves the same role as property on a normal Ext.util.Sorter.
So I tried the following, which only sorts the date correctly:
grouper:{
sortProperty: 'StartDate',
property: 'StartDateOnly',
direction: 'ASC'
},
and I tried the following, which only sorts the time correctly:
grouper:{
sortProperty: 'StartDate',
property: 'StartDateOnly',
direction: 'ASC'
},
sorters: [{
property: 'StartDate',
direction: 'ASC'
}]
You can try it here:
https://fiddle.sencha.com/#view/editor&fiddle/2cei
What am I doing wrong here?
The problem is that you make a sort on a string type but you have date by changing the code on your sencha fiddle like this :
Ext.application({
name : 'Fiddle',
launch : function() {
var mdl = Ext.define('', {
extend: 'Ext.data.Model',
fields: [{
/** #field {date} StartDate with format: Y-m-d H:i */
name: 'StartDate',
type: 'date',
dateFormat: 'Y-m-d H:i'
},{
name: 'StartDateOnly',
type: 'date', // <= here before it was 'string'
convert: function(v,rec) {
return Ext.Date.format(rec.get('StartDate'), 'D d.m.Y');
}
}]
});
...
Then you run, you can see that the group of date StartDateOnly is now order by ASC.
After that, you can retrieve this data in date format and convert it into a string. But if you make a sort on a string you will always have the first of February last like now.
The problem is that it does an alphabetical sort on the days of the week.
In order: Fri, Sat, Thu.
Sencha support did not provide a full solution, but helped me a bit on my way. They found how to "fix" the problem at hand and told me to change the data type of the StartDateOnly column to date, but neither did this fix prove universal nor did it make any sense that this fixed the problem at all. But it showed some underlying correlations, which helped me to dig into the problem and find a universal solution.
I would say the issue is a bug in ExtJS, but I am awaiting confirmation from Sencha on this. I have to yet find the exact line where the bug is introduced.
Problem description:
When sortProperty is set on the grouper and at least one sorter is added on the store, the "transform" function of the grouper is set to the "transform" function of the first sorter (otherwise, it is null).
Solution:
You can manually override the transform function on the grouper by adding the correct sort type explicitly to the grouper config:
grouper:{
sortProperty: 'StartDate',
property: 'StartDateOnly',
transform: Ext.data.SortTypes.asDate,
direction: 'ASC'
},

Dexiejs advanced search

I am building a inventory app. I have few search fields (part no, manufacturer etc..). I have been trying to build the search based on the input fields. But nothing seems to be working.
Things i tried.
db.version(1).stores({
parts: "++id,partno,description,category,manufacturer,cost,qty,add_date,condition,location"
});
Search 1:
var searchParams = { 'partno': 'md101', 'manufacturer': 'apple', 'category': 'notebook'}
Search 1:
var searchParams = { 'condition': 'new', 'category': 'notebook'}
Worked, but case sensitive. can i make it case insensitive?:
db.parts.where(search);
Failed:
var fields = Object.keys(search);
var values = Object.values(search)
db.parts.where(fields).equals(values);
Please help me to figure this out. Documentation didn't help
Thank you.
db.parts.where({
partno: 'md101',
manufacturer: 'apple',
category: 'notebook'
}).toArray().then(result => console.log(result);
The above query will make an logical and between the criterias and match exact.
To match by case, try the following :
db.parts.where('partno').equalsIgnoreCase('md101')
.and(part=>
part manufacturer &&
part manufacturer.toLowerCase() === 'apple' &&
part.category &&
part.category.toLowerCase() === 'notebook')
.toArray()
.then(result => console.log (result))
What we do here is utilizing the most spread index only and filter out the rest manually.

fetch larger images from Facebook with Koala using the get_connection method

The documentation of the Koala gem gives an example how to fetch posts from Facebook, including a picture.
client = Koala::Facebook::API.new(oauth_token)
client.get_connection('someuser', 'posts',
{limit: #options[:max_items],
fields: ['message', 'id', 'from', 'type',
'picture', 'link', 'created_time', 'updated_time'
]})
Below this example the documentation makes a note:
You can pass a ‘type’ hash key with a value of ‘small’, ‘normal’, ‘large’, or ‘square’ to obtain different picture sizes, with the default being ‘square’. Also, you may need the user_photos permission.
Unfortunately, this doesn't seem to work:
client.get_connection("officialstackoverflow", "posts",
{limit: 5, type: "large", fields: [:picture, :message, :type]})
Unfortunately, I get the same picture as I would get when omitting the type param. How do I have to pass the type hash correctly?

Mongoid Complex Query Including Embedded Docs

I have a model with several embedded models. I need to query for a record to see if it exists. the issue is that I will have to include reference to multiple embedded documents my query would have to include the following params:
{
"first_name"=>"Steve",
"last_name"=>"Grove",
"email_addresses"=>[
{"type"=>"other", "value"=>"steve#stevegrove.com", "primary"=>"true"}
],
"phone_numbers"=>[
{"type"=>"work_fax", "value"=>"(720) 555-0631"},
{"type"=>"home", "value"=>"(303) 555-1978"}
],
"addresses"=>[
{"type"=>"work", "street_address"=>"6390 N Main Street", "city"=>"Elbert", "state"=>"CO"}
],
}
How can I query for all the embedded docs even though some fields are missing such as _id and associations?
A few things to think about.
Are you sure the query HAS to contain all these parameters? Is there not a subset of this information that uniquely identifies the record? Say (first_name, last_name, and an email_addresses.value). It would be silly to query all the conditions if you could accomplish the same thing in less work.
In Mongoid the where criteria allows you to use straight javascript, so if you know how to write the javascript criteria you could just pass a string of javascript to where.
Else you're left writing a really awkward where criteria statement, thankfully you can use the dot notation.
Something like:
UserProfile.where(first_name: "Steve",
last_name: "Grove",
:email_addresses.matches => {type: "other",
value: "steve#stevegrove.com",
primary: "true"},
..., ...)
in response to the request for embedded js:
query = %{
function () {
var email_match = false;
for(var i = 0; i < this.email_addresses.length && !email_match; i++){
email_match = this.email_addresses[i].value === "steve#stevegrove.com";
}
return this.first_name === "Steve" &&
this.last_name === "Grove" &&
email_match;
}
}
UserProfile.where(query).first
It's not pretty, but it works
With Mongoid 3 you could use elem_match http://mongoid.org/en/origin/docs/selection.html#symbol
UserProfile.where(:email_addresses.elem_match => {value: 'steve#stevegrove.com', primary: true})
This assumes
class UserProfile
include Mongoid::Document
embeds_many :email_addresses
end
Now if you needed to include every one of these fields, I would recommend using the UserProfile.collection.aggregate(query). In this case you could build a giant hash with all the fields.
query = { '$match' => {
'$or' => [
{:email_addresses.elem_match => {value: 'steve#stevegrove.com', primary: true}}
]
} }
it starts to get a little crazy, but hopefully that will give you some insight into what your options might be. https://coderwall.com/p/dtvvha for another example.

Resources