format complex hash - simple_form gem - ruby

I have a nested hash like this:
LANGUAGE_DETAILS = {
BG: {
Name: 'Български',
Flag: ''
},
EN: {
Name: 'English',
Flag: ''
},
RU: {
Name: 'Руский',
Flag: ''
},
UK: {
Name: 'Украински',
Flag: ''
}
}
and need to format it like the following hash:
{
BG: 'Български',
EN: 'English',
RU: 'Руский',
UK: 'Украински'
}
in order to use it as simple_form input parameter like this:
<%= f.input :language_code, collection: SecurityUser::LANGUAGE_DETAILS,
label_method: :last,
value_method: :first,
as: :radio_buttons , label: 'Choose language' %>
Is there a way to transform the SecurityUser::LANGUAGE_DETAILS hash into new one in this context or I should create the hash on hand in the model?

You can do it like this:
Hash[LANGUAGE_DETAILS.map{|k, h| [k, h[:Name]]}]

Related

Not seeing Quill editor in active admin form

Not seeing Quill editor in active admin form.
followed all instructions from here and the whole description box doesn't diplay. same happened to other editors i tried.
form title: "Drinks" do |f|
f.inputs "Drinks" do
f.input :title
f.input :description, as: :quill_editor
f.input :steps
f.input :source
end
f.actions
end
Please ensure you have included active_admin/base to your app/assets/javascripts/active_admin.js file:
//= require active_admin/base
//= require activeadmin/quill_editor/quill
//= require activeadmin/quill_editor_input
This is what was causing the issue for me.
I have implemented Quill Rich Text Editor in Active Admin by following the below steps in Rails 6 application:
Add the gems:
gem 'activeadmin_quill_editor'
gem 'sassc'
Add below js code in app/assets/javascripts/active_admin.js:
//= require activeadmin/quill_editor/quill
//= require activeadmin/quill_editor_input
Add below css code in app/assets/stylesheets/active_admin.scss
#import 'activeadmin/quill_editor/quill.snow';
#import 'activeadmin/quill_editor_input';
For defualt toolbar options use the below syntax:
form do |f|
f.inputs 'Notices' do
f.input :description, as: :quill_editor
end
f.actions
end
For customized toolbar options the use below syntax:
form do |f|
f.inputs 'Notices' do
f.input :description, as: :quill_editor, input_html: { data: { options: { modules: { toolbar: [ ['bold', 'italic', 'underline', 'strike'],['blockquote', 'code-block'], [{ 'header': 1 }, { 'header': 2 }], [{ 'list': 'ordered'}, { 'list': 'bullet' }], [{ 'script': 'sub'}, { 'script': 'super' }], [{ 'indent': '-1'}, { 'indent': '+1' }], [{ 'direction': 'rtl' }], [{ 'size': ['small', false, 'large', 'huge'] }], [{ 'header': [1, 2, 3, 4, 5, 6, false] }], [{ 'color': [] }, { 'background': [] }], [{ 'font': [] }], [{ 'align': [] }], ['clean'] ] }, theme: 'snow' } } }
end
f.actions
end
For more information visit:
https://github.com/blocknotes/activeadmin_quill_editor
https://quilljs.com/docs/modules/toolbar/

How to merge array of hashes based on hash value but not merge values instead override

I have an array of hashes like this:
[
{ name: 'Pratha', email: 'c#f.com' },
{ name: 'John', email: 'j#g.com' },
{ name: 'Clark', email: 'x#z.com' },
]
And this is second group array of hashes:
[
{ name: 'AnotherNameSameEmail', email: 'c#f.com' },
{ name: 'JohnAnotherName', email: 'j#g.com' },
{ name: 'Mark', email: 'd#o.com' },
]
What I want is, merge these two arrays into one, merge based on :email and keep latest (or first) :name.
Expected Result (latest name overrided):
[
{ name: 'AnotherNameSameEmail', email: 'c#f.com' },
{ name: 'JohnAnotherName', email: 'j#g.com' },
{ name: 'Mark', email: 'd#o.com' },
{ name: 'Clark', email: 'x#z.com' },
]
or (first name preserved)
[
{ name: 'Pratha', email: 'c#f.com' },
{ name: 'John', email: 'j#g.com' },
{ name: 'Mark', email: 'd#o.com' },
{ name: 'Clark', email: 'x#z.com' },
]
So, basically, I want to group by :email, retain one :name, drop dupe emails.
The examples found on SO is creates an array of values for :name.
Ruby 2.6.3
Maybe you could just call Array#uniq with a block on email key of the concatenation (Array#+) of the two arrays:
(ary1 + ary2).uniq { |h| h[:email] }
a1 = [
{ name: 'Pratha', email: 'c#f.com' },
{ name: 'John', email: 'j#g.com' },
{ name: 'Clark', email: 'x#z.com' },
]
a2 = [
{ name: 'AnotherNameSameEmail', email: 'c#f.com' },
{ name: 'JohnAnotherName', email: 'j#g.com' },
{ name: 'Mark', email: 'd#o.com' },
]
Let's first keep the last:
(a1+a2).each_with_object({}) { |g,h| h.update(g[:email]=>g) }.values
#=> [{:name=>"AnotherNameSameEmail", :email=>"c#f.com"},
# {:name=>"JohnAnotherName", :email=>"j#g.com"},
# {:name=>"Clark", :email=>"x#z.com"},
# {:name=>"Mark", :email=>"d#o.com"}]
To keep the first, do the same with (a1+a2) replaced with (a2+a1), to obtain:
#=> [{:name=>"Pratha", :email=>"c#f.com"},
# {:name=>"John", :email=>"j#g.com"},
# {:name=>"Mark", :email=>"d#o.com"},
# {:name=>"Clark", :email=>"x#z.com"}]

Mongoid: unique index for embedded documents

I'm trying to create a unique field for embedded documents:
class Chapter
include Mongoid::Document
field :title
end
class Book
include Mongoid::Document
field :name
embeds_many :chapters
index({ 'name' => 1 }, { unique: true })
index({ 'name' => 1, 'chapters.title' => 1 }, { unique: true, sparse: true })
# index({ 'name' => 1, 'chapters.title' => 1 }, { unique: true })
end
I run the task: rake db:mongoid:create_indexes
I, [2017-02-22T08:56:47.087414 #94935] INFO -- : MONGOID: Created indexes on Book:
I, [2017-02-22T08:56:47.087582 #94935] INFO -- : MONGOID: Index: {:name=>1}, Options: {:unique=>true}
I, [2017-02-22T08:56:47.087633 #94935] INFO -- : MONGOID: Index: {:name=>1, :"chapters.title"=>1}, Options: {:unique=>true, :sparse=>true}
But it doesn't work as I would expect...
Book.new( name: 'A book', chapters: [ { title: 'title1' }, { title: 'title1' }, { title: 'title2' } ] ).save # no errors
Book.new( name: 'Another book', chapters: [ { title: 'title2' } ] ).save
b = Book.last
b.chapters.push( Chapter.new( { title: 'title2' } ) )
b.save # no errors
Any idea?
UPDATE: Ruby 2.4.0, Mongo 3.2.10, Mongoid 5.2.0 | 6.0.3 (trying both)
UPDATE2: I add also the tests I made directly with mongo client:
use books
db.books.ensureIndex({ title: 1 }, { unique: true })
db.books.ensureIndex({ "title": 1, "chapters.title": 1 }, { unique: true, sparse: true, drop_dups: true })
db.books.insert({ title: "Book1", chapters: [ { title: "Ch1" }, { title: "Ch1" } ] }) # allowed?!
db.books.insert({ title: "Book1", chapters: [ { title: "Ch1" } ] })
b = db.books.findOne( { title: 'Book1' } )
b.chapters.push( { "title": "Ch1" } )
db.books.save( b ) # allowed?!
db.books.findOne( { title: 'Book1' } )
db.books.insert({ title: "Book2", chapters: [ { title: "Ch1" } ] })
UPDATE3: I made more tests but I didn't succeed, this link helped but the problem remains
You should use drop_dups
class Category
include Mongoid::Document
field :title, type: String
embeds_many :posts
index({"posts.title" => 1}, {unique: true, drop_dups: true, name: 'unique_drop_dulp_idx'})
end
class Post
include Mongoid::Document
field :title, type: String
end
Rails console:
irb(main):032:0> Category.first.posts.create(title: 'Honda S2000')
=> #<Post _id: 58adb923cacaa6f778215a26, title: "Honda S2000">
irb(main):033:0> Category.first.posts.create(title: 'Honda S2000')
Mongo::Error::OperationFailure: E11000 duplicate key error collection: mo_development.posts index: title_1 dup key: { : "Honda S2000" } (11000)

Ruby map specific hash keys to new one

I've got an array full of hashes of which I want to combine specific keys to a new one, e.g.
[{ firstname: 'john', lastname: 'doe', something: 'else', key: ... }, { firstname: 'Joe', lastname: 'something', something: 'bla', key:... }]
should become
[{ name: 'john doe' },{ name: 'Joe something' }]
Please note: there are more keys in the hash as first and lastname. Is there a common ruby method to do this? Thanks!
Just do as
array = [{ firstname: 'john', lastname: 'doe' }, { firstname: 'Joe', lastname: 'something' }]
array.map { |h| { :name => h.values_at(:firstname, :lastname) * " " } }
# => [{:name=>"john doe"}, {:name=>"Joe something"}]
Read this Hash#values_at and Array#* .
This is:
a = [{ firstname: 'john', lastname: 'doe' }, { firstname: 'Joe', lastname: 'something' }]
a.map { |n| { name: n.values.join(' ') } }
# => [{:name=>"john doe"}, {:name=>"Joe something"}]

What is this format in ruby?

In a book they showed me this declaration:
friends = [ { first_name: "Emily", last_name: "Laskin" }, { first_name: "Nick", last_name: "Mauro" }, { first_name: "Mark", last_name: "Maxwell" } ]
This doesn't look like a hash. And when I enter it in IRB i get an error.
What is this format?
It's an array of hashes, written in the Ruby 1.9 hash syntax.
{ first_name: "Emily", last_name: "Laskin" }
is equivalent to:
{ :first_name => "Emily", :last_name => "Laskin" }
The {key: value} syntax is new in 1.9 and is equivalent to {:key => value}.
It is an array of hashes, only the hashes are 1.9 style hashes.

Resources