Posts

Showing posts with the label ruby

Rails : How to put I18N into SVG file

Rails : How to put I18N into SVG file I want to internationalize several SVG files but I have no idea how I could do that. Here's my SVG file : <text font-family="OpenSans-Semibold, Open Sans" font-size="22" font-weight="500" fill="#FFFFFF"> <tspan x="39" y="157">Accessoires</tspan> </text> Should I do something like : <text font-family="OpenSans-Semibold, Open Sans" font-size="22" font-weight="500" fill="#FFFFFF"> <tspan x="39" y="157"><%= t('accessoires') %></tspan> </text> Or is there an other way to do it ? Thanks for your answers. @RobertLongson what kind of joke is that ? – Enner31 Jul 2 at 13:07 1 Answer ...

Ruby on Rails — Stack level too deep

Ruby on Rails — Stack level too deep model: after_save :set_correct_post_type def set_correct_post_type if self.document.present? if (find_mime_type(self.document.original_filename) == "application/vnd.openxmlformats-officedocument.presentationml.presentation") || (find_mime_type(self.document.original_filename) == "application/vnd.ms-powerpoint") self.update_attributes(:post_type => 3) elsif (find_mime_type(self.document.original_filename) == "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ) || (find_mime_type(self.document.original_filename) == "application/msword") || (find_mime_type(self.document.original_filename) == "application/pdf") self.update_attributes(:post_type => 2) elsif (MIME::Types.type_for(self.document.original_filename).first.content_type == "image/png") || (MIME::Types.type_for(self.document.original_filename).first.content_type =="image/jpeg...

In ruby, why is Set unordered while Hash is guaranteed in insertion order?

In ruby, why is Set unordered while Hash is guaranteed in insertion order? Hash in ruby 2.5.1: Hashes enumerate their values in the order that the corresponding keys were inserted. Set in ruby 2.5.1: Set implements a collection of unordered values with no duplicates. I thought set was implemented as a Hash in ruby. Then why is Set unordered while Hash is guaranteed in insertion order? UPDATE: I understand that mathematically sets are supposed to be unordered. An important question arises, even though the Set class is actually in insertion order, it is not documented. I think the documentation should be updated. Set Even in the Set#add source, we can see it's actually using Hash : Set#add Hash # File set.rb, line 348 def add(o) @hash[o] = true self end Note: I don't mean to spark off discussion, but think it's important that this fact is documented somewhere. If not in the official docs, atleast on stack overflow. The fact that sets are imp...

Singleton class of singleton class of BasicObject in Ruby

Image
Singleton class of singleton class of BasicObject in Ruby This is mostly an "academic" one but here it goes: According to the ruby eigenclass diagram (slightly edited) here: BasicObject.singleton_class.singleton_class.superclass is Class . BasicObject.singleton_class.singleton_class.superclass Class However running this on a ruby interpreter it turns out that BasicObject.singleton_class.singleton_class.superclass is #Class and not Class . Therefore, is the diagram lying or am I missing something? BasicObject.singleton_class.singleton_class.superclass #Class Class Thanks. BasicObject.singleton_class.singleton_class.is_a?(Class) #⇒ true #Class is a string representation. – mudasobwa Jul 2 at 8:59 BasicObject.singleton_class.singleton_class.is_a?(Class) #⇒ true #Class Your question says BasicObject.si...

How to fetch parents based on their child's records

How to fetch parents based on their child's records I have two models like company.rb class Company < ApplicationRecord has_many :posts end post.rb class Post < ApplicationRecord belongs_to :company scope :notClosed, -> {where(closed: false)} scope :published, -> {where(published: true)} end I want to fetch companies which carries at least one post with matching post scope scope Currently my queries is Company.where(company_type: "Private").all It's return all companies but how to modify this query for my needs. I want to fetch companies which carries at least one post , actually, an INNER JOIN keyword would return the companies with posts associated, you can append a distinct statement to get non-repeated records. Can you explain further what you're trying to do? – Sebastian Palma Jul 2 at 3:55 ...

Rails routes do not contain id in that

Rails routes do not contain id in that I use rails 5 and setting route, type rake routes in console and result is: Prefix Verb URI Pattern Controller#Action new_report_templates GET /report_templates/new(.:format) report_templates#new edit_report_templates GET /report_templates/edit(.:format) report_templates#edit report_templates GET /report_templates(.:format) report_templates#show PATCH /report_templates(.:format) report_templates#update PUT /report_templates(.:format) report_templates#update DELETE /report_templates(.:format) report_templates#destroy POST /report_templates(.:format) report_templates#create It's so weird to see that result routes do not have id in url template. My route: Rails.application.routes.draw do resource :report_templates end I use gem 'rails', '~> 5.1.6'. So my question is: why my result routes d...

Not able to add DataValidation in rubyXL while writing to excel

Not able to add DataValidation in rubyXL while writing to excel I am using rubyXL gem to write to excel sheet. I need to add a data validation for a column which has dropdown select i.e (List type). To implement this, I see in rubyXL class we have data_validation.rb data_validation.rb gems data_validation code which has type to array of values of ST_DataValidationType = %w{ none whole decimal list date time textLength custom } From this,I don't find a lead to write a dropdown list in the worksheet. Could someone guide me to implement this dropdown inside the excel sheet? By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

How do I count unread comments using “Unread” Ruby gem?

How do I count unread comments using “Unread” Ruby gem? comments_helper.rb : module CommentsHelper def unread_comments_count @comments_count = Comment.unread_by(user).count end end In my ApplicationController , I defined user : ApplicationController user def user @user = User.find_by(params[:user_id]) end I could have used current_user , but the result I got (with that) is total number of comments in my database. So, I thought I should use user . But it didn't work. current_user user view : <%= unread_comments_count %> However, the result is still total number of all the comments made on all the posts by all users. I can't actually figure out what I am doing wrong because I have carefully done everything right in my Models as guided by the gem. I would appreciate any clue to fix this. Update The query it generates is: SELECT COUNT(*) FROM "comments" 'LEFT JOIN read_marks ON read_marks.readable_type = "Message" AND read_marks.readable_id ...

Combine for loop with cat and json2html

Combine for loop with cat and json2html I try to convert json files to html files. So I can later import them to the website. My idea is run json2table to create a table and write then to the html directory. But I find not out how can redirecting the output of json2table. for file in $(ls json/*.json); do cat $file | json2table > html/$file.html; done But this end with bash: html/json/26.json.html: No such file or directory Is there someone which can help to fix the error? Thank you Silvio Check content of $file and html/$file.html in your loop. – Cyrus Jul 1 at 19:27 $file html/$file.html 3 Answers 3 Your ${file} includes the json directory. When you don't have a directory html/json the redirection will fail. I think you have a subdir html ...

Process Jekyll content to replace first occurrence of any post title with a hyperlink of the post with that title

Process Jekyll content to replace first occurrence of any post title with a hyperlink of the post with that title I am building a Jekyll ruby plugin that will replace the first occurrence of any word in the post copy text content with a hyperlink linking to the URL of a post by the same name. I've gotten this to work but I can't figure out two problems in the process_words method: process_words post.data['url'] The current code works but will replace the first occurrence even if it's the value of an HTML attribute, like an anchor or a meta tag. We have a blog with 3 posts: And in the "Hobbies" post body text, we have a sentence with each word appearing in it for the first time in the post, like so: I love mountain biking and bicycles in general. The plugin would process that sentence and output it as: I love mountain biking and <a href="https://example.com/link/to/bicycles/">bicycles</a> in general. # _plugins/hyperlink_first_word_...

Working with Transpose functions result in error

Working with Transpose functions result in error consider the following array arr = [["Locator", "Test1", "string1","string2","string3","string4"], ["$LogicalName", "Create Individual Contact","value1","value2"]] Desired result: [Test1=>{"string1"=>"value1","string2"=>"value2","string3"=>"","string4"=>""}] When I do transpose, it gives me the error by saying second element of the array is not the length of the first element in the array, Uncaught exception: element size differs (2 should be 4) so is there any to add empty string in the place where there is no element and can perform the transpose and then create the hash as I have given above? The array may consist of many elements with different length but according to the size of the first element in the array, every other inner array ...

Best way to save attribute inside save block?

Best way to save attribute inside save block? In my controllers I often have functionality like this: @account = Account.new(account_params) if @account.save if @account.guest? ... else AccountMailer.activation(@account).deliver_later @account.update_column(:activation_sent_at, Time.zone.now) flash[:success] = "We've sent you an email." redirect_to root_path end end What's the best way to send an email and update the activation_sent_at attribute without having to save the record twice? Calling update_column doesn't feel right to me here because AFAIK it creates an extra SQL query (correct me if I'm wrong). activation_sent_at update_column The workaround is using assign_attributes , but I would not recommend it. Your code is fine, and an additional update query to update the column shouldn't make much difference. Also, check like guest? can also be applicable on some model hooks which won't be in...