Loop JSON objects in Ruby On Rails - ruby-on-rails

I am trying to figure out how to display custom HTML objects created by users. For example, the user can create custom styled button(s) with the following properties: color, font-size, and border-radius. Here is an example of my json:
[
{
id: 2,
configs: {
color: "#000000",
font_size: 10,
border_radius: 0,
display: true,
button_1: [
{
color: "red",
font_size: 12,
border_radius: 4,
display: true,
}
],
},
created_at: "2020-05-07T21:42:04.808Z",
updated_at: "2020-05-07T21:42:04.808Z",
},
{
id: 3,
configs: {
color: "#000000",
font_size: 10,
border_radius: 0,
display: true,
button_1: [
{
color: "blue",
font_size: 15,
border_radius: 24,
}
],
button_2: [
{
color: "pink",
border_radius: 2,
}
],
},
created_at: "2020-05-07T22:29:31.255Z",
updated_at: "2020-05-07T22:29:31.255Z",
},
]
I want to display all buttons according to the user's style settings. Tried something like:
%p #{btn.id}: #{btn[:configs][:button_1][0][:color][:font_size]}
But it doesn't look practical. Besides, in certain cases it can lead to the error:
undefined method `[]' for nil:NilClass
I've tried to use gem like activerecord-typedstore but it seems like it doesn't support nested attributes (Is there a way to master a nested json with a json column?). Is there any way to display custom button(s) based on the user's criteria?

In Ruby "configs" and :configs are not equivalent. You must use the type employed in the structure.
To navigate complex JSON structures use dig:
btn.dig('configs', 'button_1', 0, 'color', 'font_size')
You can also symbolize the keys if you want using symbolize_keys:
btn.symbolize_keys!
If you prefer symbols and you're reading in the JSON manually you can always request it with symbol keys out of the gate using the symbolize_names option to JSON.parse:
JSON.parse('{"test": true}')
# => {"test"=>true}
JSON.parse('{"test": true}', symbolize_names: true)
# => {:test=>true}

Related

How to validate text in Extjs6 ? and where to add the code?

I was following tutorial in Extjs , i had this view (YourTurn.js)
Ext.define('Student.view.main.YourTurn', {
extend: 'Ext.window.Window',
xtype: 'mainyour',
requires: [
'Student.view.main.YourTurnController',
'Ext.form.Panel'
],
autoShow: true,
height: 170,
width: 360,
layout: {
type: 'fit'
},
iconCls: 'key',
title: "Login",
closeAction: 'hide',
closable: false,
items: [{
xtype: 'form',
frame: false,
bodyPadding: 15,
defaults: {
xtype: 'textfield',
anchor: '100%',
labelWidth: 60
},
items: [{
name: 'user',
fieldLabel: "User",
allowBlank:false,
vtype:'alphanum',
minLenght:3,
msgTarget:'under',
}, {
inputType: 'password',
name: 'password',
fieldLabel: "Password",
maxLength:15,
allowBlank:false,
vtype:'alphanum',
minLenght:3,
msgTarget:'under',
}]
}]
});
and I had the controller of this view (YourTurnController.JS) like this :
Ext.define('Student.view.main.YourTurnController', {
extend: 'Ext.app.ViewController',
alias: 'controller.yourturncontroller',
Ext.apply(Ext.form.field.VTypes, {
customPass: function(val, field) {
return /^((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%]).{6,20})/.
test(val);
},
customPassText: 'Not a valid password. Length must be at least
6 characters and maximum of 20 Password must contain one digit,
one
letter lowercase,
one letter uppercase,
onse special symbol # # $ % and
between 6 and 20 characters.
',
});
Its should validate that the enter vlaue to the textfield should be between 0-9,with small and capital letter..etc , but it did not working i got this error in brower (chrome) :
YourTurnController.js?_dc=1477341018794:5 Uncaught SyntaxError: Unexpected token .
ext-all-rtl-debug.js?_dc=1477341016439:9126 [E] [Loader] The following classes failed to load:log # ext-all-rtl-debug.js?_dc=1477341016439:9126logx # ext-all-rtl-debug.js?_dc=1477341016439:9162Ext.apply.log.log.error # ext-all-rtl-debug.js?_dc=1477341016439:9165(anonymous function) # ext-all-rtl-debug.js?_
please can any one help me to understadn what i did wrong ?
you should consider using a listener to validate the input type of your password.
Try this link to the doc for the change listener:
http://docs.sencha.com/extjs/6.2.0/classic/Ext.form.field.Text.html#event-change

Nested/Sub Tables with PDFMake

How do I use nested/sub tables with PDFmake? I've tried simply putting in multiple tables but that doesn't automatically repeat the top level table's header for page breaks.
This code is a simplified example of using a sub-table. It is adapted from tables section of the pdfmake playground (wasn't easy to find via Google searching).
Paste the following into: http://pdfmake.org/playground.html
// playground requires you to assign document definition to a variable called dd
var dd = {
content: [
{ text: 'A simple table with nested elements', style: 'subheader' },
'It is of course possible to nest any other type of nodes available in pdfmake inside table cells',
{
style: 'tableExample',
table: {
headerRows: 1,
body: [
['Column 1', 'Column 2'],
[
{
stack: [
'Let\'s try an unordered list',
{
ul: [
'item 1',
'item 2'
]
}
]
},
[
'or a nested table',
{
table: {
body: [
[ 'Col1', 'Col2', 'Col3'],
[ '1', '2', '3'],
[ '1', '2', '3']
]
},
}
]
]
]
}
},
]
}
I need to achieve almost similar functionality like above. I have tried a lot, but not getting it right. How to create a json array that would produce an pdf output using pdfmake? I need to create a table within a table:
var dd = {
content: [
{
text: 'A simple table with nested elements',
style: 'subheader'
},
'It is of course possible to nest any other type of nodes available in pdfmake inside table cells',
{
style: 'tableExample',
table: {
headerRows: 1,
body: [
['Column 1', 'Column 2'],
[
[
'or a nested table',
{
table: {
body: [
[ 'Col1', 'Col2', 'Col3'],
[ '1', '2', '3'],
[ '1', '2', '3']
]
},
}
]
]
]
}
},
]
}

How to display total of recordes in jqxgrid

I have jqxgrid.I am very new to jqxgrid.I want to display total of recordes or values in column.
Can any tell me how to do
enter image description here
jqxgrid have feature to show aggregates for some column, that aggregates could be 'SUM', 'COUNT', or 'AVERAGE'
you can set the aggregates types when u set the column setting in jqxgrid declaration.
for simple example is like this :
// initialize jqxGrid
$("#jqxgrid").jqxGrid(
{
width: 850,
source: dataAdapter,
showstatusbar: true,
statusbarheight: 50,
showaggregates: true,
columns: [
{ text: 'First Name', columntype: 'textbox', datafield: 'firstname', width: 170 },
{ text: 'Last Name', datafield: 'lastname', columntype: 'textbox', width: 170 },
{ text: 'Price', datafield: 'price', cellsalign: 'right', cellsformat: 'c2', aggregates: ['sum', 'avg'] }
]
});
for custom aggregates, u can learn from this link
http://www.jqwidgets.com/jquery-widgets-demo/demos/jqxgrid/aggregates.htm?arctic
for complete documentation for aggregates please check the API's documentation from their documentation.
hope this helps

jQuery Select2 - Highlighting the child (when the search term matches) instead of its parent in hierarchical list

Select2 3.5.2.
I have some hierarchical data in a select2 list where the parents AND children are all valid selections. If possible, when the search term matches a child, I'd like the child to be highlighted by default instead of the parent.
For example, given the following code...
$("#hdn").select2(
{
width: '300px',
data:
[
{
id: 1,
text: 'Italy',
children:
[
{ id: 2, text: 'Italy - Sardinia' },
{ id: 3, text: 'Italy - Sicily' },
]
},
{
id: 4,
text: 'United Kingdom',
children:
[
{ id: 5, text: 'United Kingdom - Guernsey' },
{ id: 6, text: 'United Kingdom - Jersey' }
]
}
]
});
... if you start typing 'Jer', it currently highlights 'United Kingdom' by default:
Ideally, if you start typing 'Jer', it should highlight 'United Kingdom - Jersey' by default instead.
Because this is a group I still want the parent to show as an option, I just want the child to be highlighted by default instead.
See this fiddle: http://jsfiddle.net/moo_ski_doo/atnph13b/2/
Select2 will highlight the first option which is selectable by default. If you don't want the highlight to sit on "United Kingdom" first, you are going to have to remove the id.
Select2 3.5.2 does not provide an easy way to change how what option is highlighted by default.

Customizing the tooltip with chartkick in Ruby on Rails

I'm doing a rails app and was able to get chartkick working but customizing it is a bit of a pain in the butt. I'm trying to customize the tooltip with the Google chart API but I can't seem to figure out. I just want to be able to change the text, "Value".
Could someone put me in the right direction.
Here is my code:
<%=
column_chart [
["Strongly Disagree", p[1]],
["Disagree", p[2]],
["Neutral", p[3]],
["Agree", p[4]],
["Strongly Agree", p[5]]
],
height: "220px",
library: {
width: 665,
fontName: "Helvetica Neue",
colors: ["#29abe2"],
tooltip: {
textStyle: {
color: "#333333"
}
},
bar: {
groupWidth: "50%"
},
vAxis: {
title: "Everyone",
titleTextStyle: {
italic: false,
color: '#777'
},
gridlines: {
color: "#eeeeee"
},
viewWindow: {
max: m
}
}
}
%>
The chartkick documentation is sparse, but if this system works the way I suspect it does, then you need to add an array of column headers to the start of your data, eg:
[
["Label for column 0", "Label for column 1"],
["Strongly Disagree", p[1]],
["Disagree", p[2]],
["Neutral", p[3]],
["Agree", p[4]],
["Strongly Agree", p[5]]
]

Resources