How to use I18n.locale with Hash#dig - ruby

I have the following Hash:
h = {
forms: {
version1: {
href: {
en: 'https://form1_en.example.org',
de: 'https://form1_de.example.org'
}
},
version2: {
href: {
en: 'https://form2_en.example.org',
de: 'https://form2_de.example.org'
}
}
}
}
I am trying to access verion1 href for the user's current locale. So what I do is:
Proj.config.forms.dig('version1', 'href', I18n.locale)
However, this results in nil even though I18n.locale returns en.
This, however, works perfectly fine and returns the correct href.
Proj.config.forms.dig('version1', 'href', 'en')
What am I doing wrong here? Can I not use I18n.locale in connection with hash#dig?

I think that possible problem might be if I18n.locale returns :en instead of 'en'. In this case you can solve it in a few ways:
Proj.config.forms.dig('version1', 'href', I18n.locale.to_s)
or
Proj.config.forms.with_indifferent_access.dig('version1', 'href', I18n.locale)

Related

Yup validation with one field but two possible options (OR)

I am trying to create an input that takes a string that will be used as the href value for a tag. The href can be a url OR an email (for mailto:).
It works if I just check for email, or if I just check for URL. However, I want to check for one or the other. I am looking through yup documentation but I can't find a way to do an OR.
I noticed that there is a when to test for another field but I'm not checking if another field is true or not, or use test but I also can't seem to get it to work.
const vSchema = yup.object().shape({
text: yup.string().required(),
href: yup
.string()
.email('Link must be a URL or email')
.url('Link must be a URL or email')
.required('Link is a required field'),
});
test this
yup.addMethod(yup.string, "or", function(schemas, msg) {
return this.test({
name: "or",
message: "Please enter valid url or email." || msg,
test: value => {
if (Array.isArray(schemas) && schemas.length > 1) {
const resee = schemas.map(schema => schema.isValidSync(value));
return resee.some(res => res);
} else {
throw new TypeError("Schemas is not correct array schema");
}
},
exclusive: false
});
});

Creating a FactoryBot Hash model with complicated structure, including multiple nested attributes

I'm implementing a set of Cucumber driven API tests and stumbled into one specific model that has a few nested elements, like this:
factory :human_being, class: Hash do
human {
name {
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
}
age { rand(1..100) }
}
initialize_with { attributes }
end
The result I wanted to achieve should look like this:
{
:human => {
:name => {
:first_name => "mike",
:last_name => "blob"
},
:age => 16
}
}
I am invoking creation using FactoryBot.build(:human_being) and I get undefined method 'name' for #<FactoryBot::SyntaxRunner:0x00007fd640a39a80>
My env.rb has the World(FactoryBot::Syntax::Methods) / FactoryBot.find_definitions lines.
I've lurked through a few answers regarding the nested attributes / associations / traits but I didn't find a proper way to get what I want. Sorry if the post is duplicated and thanks in advance for help.
Okay, seems that I've found the solution right after posting the question. Currently the following model creates the Hash I described in the question:
factory :human_being, class: Hash do
human { {
name: {
first_name: Faker::Name.first_name,
last_name: Faker::Name.last_name,
},
age: rand(1..100),
sex: %w(male female other).sample
} }
initialize_with { attributes }
end
Although I'm not sure it is a completely correct answer from guidelines perspective, but at least it works for my exact case.
source (See 'Defining factories'): Because of the block syntax in Ruby, defining attributes as Hashes (for serialized/JSON columns, for example) requires two sets of curly brackets:
factory :program do
configuration { { auto_resolve: false, auto_define: true } }
end

CouchDB get all Special Characters

I need to get all the special characters from a view.
I have documents like this:
doc.name: ""
doc.name: " "
doc.name: "+Name"
doc.name: "*Name"
I try this params:
startkey: 'null', endkey: '#'
and
startkey: '\u0000', endkey: '\u0040'
But I can't get the contacts starting with '+' or '*'.
This is my view:
"function(doc) { if (doc.name) { emit(doc.name.charAt(0).toLowerCase(), doc) }}"
My final idea is get all characters not beginning with letters.
How many request I need to do for this and which params should I use?
Ok, I find the way to do this, first to get all special characters I do:
startkey: '', endkey: '9'
But like this I don't get the doc when the key don't exist or is null, because my view, so I change my view to:
"function(doc) { if (doc) { if (doc.name)
{ emit(doc.name.charAt(0).toLowerCase(), doc) } else { emit('', doc) }
} }"
And like this I can get everything.
I don't know if this is the best way to do this, but it's working.

How do I add anchor text to a link in an HTML document I am building with Nokogiri?

All I want to be able to do is to create an HTML page with a list of links and names/text associated with that text.
E.g. Google
Where I can change Google to be any text I want (including data from a variable).
I have this:
builder = Nokogiri::HTML::Builder.new do |doc|
doc.html {
doc.body {
contents.each do |i|
doc.p {
doc.a(:href => list.first)
}
end
}
}
end
This just produces this:
<html><body><p></p></body></html>
What I want that to be though is:
<html><body><p>First Link</p></body></html>
How do I do that in Nokogiri?
Thanks.
doc.a 'text_goes_here', :href => 'href_goes_here'
Actually...just figured it out.
All I have to do is add doc.text "First Link".
So the updated code snippet would look like this:
builder = Nokogiri::HTML::Builder.new do |doc|
doc.html {
doc.body {
contents.each do |i|
doc.p {
doc.a(:href => list.first) {
doc.text "First Link"
}
}
end
}
}
end
Works like a charm.

jquery.tokeninput required

I have this fantastic little plugin working but I need to require that at least one name be selected. I normally use jquery.validate. However, the validation plugin does not appear to work on a field using the tokeninput. Does anyone have an answer? As always, thanks so much for your help.
$("#NewMessage").validate({
rules: {
name: {
required: true
}
}
});
$("#name").tokenInput("lookup.cfc?method=getNames&returnFormat=json", {
hintText: "Type in the name of recipient(s)",
noResultsText: "No results",
searchingText: "Searching..."
})
I had the same issue and solved it overriding ignore (apparently the problem is that tokenInput hides the original input and by default "validate" doesn't validate hidden inputs)
$("#NewMessage").validate({
ignore: "",
rules: {
name: {
required: true
}
}
});
I wrote and tried,It's working fine to me.
jQuery.validator.addMethod("autocomplete_check", function(value, element) {
return ( value != '' ) ? true : false;
}, "");
$("#NewMessage").validate({
ignore: "",
rules: {
name: {
autocomplete_check: true
}
},
messages: {
autocomplete_check: "Please fill the name"
}
});

Resources