issue in passign hash values across objects in ruby onr rails - ruby-on-rails-3

I am running into problems passing a hash (serialized as a hash) to a newly created object. The values get converted to yaml format.
Consider the following:
Model ComputerUser:
...
serialize preferences
'#in the database I see the following "{0=>{color:red format:html}}"
....
#computer_user.registrations.build(:user_pref => :preferences}.save
#computer_user.user_pref;
'#the above statement spills out the data in yaml format and that is how it gets persisted in the db.
Now, if I do the following from rails console, I don't see the same issue, i.e. the hash is stored as a hash and not converted to the yaml format. I see the following when I inspect the value of the column in the new object:
=>{0=>{color:red format:html}}
Please note that I have used serialize for the attributes in the source as well as the target. Things seem to work from the console but just note from the controller! Any ideas what is going on? Why is the issue occurring only in the web application and not on the console.

The issue was that I was not using instance variables for assignment.
If you have "serialize, Hash" declared in both classes, this is how you need to build the child object from the parent:
#computer_user.registrations.build(:user_prefs => #computer_user.preferences).save
That works like a charm.

Related

Bind Key Vault settings to class

In ASP.NET Core, if reading configuration from a JSON app.settings file I can bind a section to an object like this:
services.Configure<MyPocoConfig>(Configuration.GetSection("Section"))
Is there a straightforward way to do this with a group of settings that are read from Azure Key Vault? I am following the guide as described in the MSDN documentation here https://learn.microsoft.com/en-us/azure/key-vault/vs-key-vault-add-connected-service#access-your-secrets-in-code
I can manually map them like this:
services.Configure<MyPocoConfig>(myPoco =>
{
myPoco.Option1 = Configuration["Option1"];
myPoco.Option2 = Configuration["Option2"];
});
I just wondered if there was a way to automap them as it works for config stored in app.settings JSON. I'm sure it could be done with reflection but I was hoping there'd be a built in way.
I tried putting the settings into a category using the category--setting syntax described in the article and reading them with services.Configure<MyPocoConfig>(Configuration.GetSection("category")), but this doesn't work.
Edit:
It is now possible as of 2020 to put settings into a category using the category--setting syntax and read them like services.Configure<MyPocoConfig>(Configuration.GetSection("category"))
You can achieve the same by naming your Secret in the following pattern.
Section--Option1
Section--Option2
And you can use the following to get the values by section and .NetCore automatically maps it.
services.Configure<MyPocoConfig>(Configuration.GetSection("Section"))
Refer link https://learn.microsoft.com/en-us/aspnet/core/security/key-vault-configuration?view=aspnetcore-2.2#bind-an-array-to-a-class

PostgreSQL & DataMapper issues

Been scratching my head for a while on this one and despite trying many variations I cannot see the mistake. After writing the app file, which contains what looks like the correct DataMapper.setup code for using PostgreSQL (?), and upon trying to play around in IRB/PRY, i just get a 'FATAL database not created' message even after i have called 'Song.auto_migrate!', here is my code, can anyone help me get past this? Thanks in advance:
require 'data_mapper'
require 'dm-core' #main DataMapper gem
require 'dm-migrations' #extra DataMapper functionality extension
DataMapper.setup(:default, "postgres://localhost/development")
class Song
include DataMapper::Resource
property :id, Serial
property :title, String
property :lyrics, Text
property :length, Integer
property :released_on, Date
end
DataMapper.finalize
I require the file all fine in irb, then call Song.auto_migrate! and it runs the 'database does not exist' error. What am i doing wrong?
You need to do this in command line:
psql
and then
CREATE DATABASE development;
before even trying to run Data Mapper setup code.
Perhaps you are missing this line: DataMapper.auto_upgrade!
On a side note, remember that auto_migrate! will wipe any existing data, whereas auto_upgrade! doesn't: http://datamapper.org/getting-started.html

How to use Global Property name in my JSON input request using SoapUI?

I have a SoapUI project which contains around 60 plus services. Each service requires some input which will be changed for every execution. So I have created certain Global Properties and assign some values to that properties.
I have to use these properties values in my SoapUI request ( i.e. JSON Format request ).
If it is groovy script means, I will use like this.
String HTiC_Username = com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils.globalProperties['HTiC_Username'].value;
But, how to get the value of the Global Property in the request?
Hope you understand my question. Please provide proper guidance.
Thanks
To dynamically "expand" (i.e. substitute) the value of a property into a test step, the following syntax is used: ${#scope#propertyName}
...where 'scope' refers to the level at which the property has been defined (e.g. Global, Project, TestSuite, TestCase).
So to expand a property named username defined as a Global property, for example, the following code can be used directly within a Request Test Step (e.g within a JSON body, or header value, etc):
${#Global#username}
To access the same property value within a Groovy Test Step, you can use the following syntax:
context.expand('${#scope#propertyName}')
...as in the following example:
context.expand('${#Global#username}')
What we did was the following:
created a test data file to store all the specific input data for the different services (testdata.properties)
Example content of testdata.properties:
Billing_customerID=1234567
OtherService_paymentid=12121212
....
create a SoupUi global parameter (File/Preferences/Global properties): testdata_filepath=C:\...
For specific services we added a Properties test step. You can specify the "Load from" field to our new global parameter: ${#Global#testdata_filepath} Now you can use the Load button to load parameters.
Finally you can reference the parameter in your xml in the following format: ${Properties#Billing_customerID}
Example content of a service with parameter:
...
<BillingCustomerIdentification>
<BillingCustomerID>${#Properties#Billing_customerID}</BillingCustomerID>
</BillingCustomerIdentification>
...
To set up your projects in this manner also helps to automate service tests eg. using Hudson (see my previous SO answer).
If it is too heavy and automation is not a target, you can simply use ${#Global#someinputvariable} format in your xml ;-)

Validate number of attributes with date_select

I'm doing something like the following: Validate number of nested attributes.
I'm checking for existence of at least one nested attribute.
This was working fine when I was using a text input for the date but I've changed it to use a date_select instead and now the same validation code shows an error saying that not enough have been chosen.
When it fails validation and reloads the form it also doesn't "build" an instance of the nested attribute either so it just shows my "[+]" link
Anybody got any ideas?
Failing everything I'll just have to put the text field back (probably using type=date).
Col
I decided to just put the text field back.

Ruby on Rails 3: rails_admin + puret?

Did someone try to integrate puret into rails_admin? I can't make a language switch to edit different translations :(
Changing I18n.locale forces whole rails_admin to use specified locale.
Now I got the solution. The two can work together well. In short:
Delete the pureted column(s) in your model
If you have the column pureted still in your model, rails form helper will bypass puret. That is, if a model called Post has a field called contents to be i18ned, the table posts SHOULD NOT have the column contents.
Actually we should use globalize3 instead. With this you do not need to remove the original column. And puret doens't support nested attributes assignment. globalize3 works very well.