vendredi 17 juin 2016

i am going to creating simple mailer but when user saved, email is not send to users email

create method looks like this and github link is http://ift.tt/1YwKYk9

def create @user = User.new(user_params) if @user.save # Deliver the signup email UserNotifier.send_signup_email(@user).deliver redirect_to(@user, :notice => 'User created') else render :action => 'new' end end

what is the best way to loop through large data with insert query in rails

i have to insert large data in lets say 20k i doubt i have written an optimised query.

users = MergeField.get_user_field_values(notification_template.merge_field,scope_users) **#returns 20k users**
    if users.present?
          users.each_slice(100) do |record|
            record.each do |user_record|
              user = User.find_by(id: user_record.user_id)
              text = notification_template.title
              notification_template.description = MustacheDescription.render(notification_template,user_record)
              text += " " + notification_template.description
              Rails.logger.info  "Merge field message: #{notification_template.description}"
              construct_user_notifications(notification_template,user_record.user_id) **#=> this calls another method below which actually create notifications for every user.**
              badge = (notification_template.display_screen == "suggestion") ? user.unread_suggestion_notifications.count : user.unread_option_notifications.count
              devices = user.devices.with_notification_token
              if devices.present?
                devices.each do |device|
                  PushNotification.notify_ios(text,device.notification_token,badge,{screen: notification_template.display_screen})
                  Rails.logger.info  "Sending push to user_id #{user_record.user_id} token #{device.notification_token}"
                end
              end
            end
          end
        end

def self.construct_user_notifications(notification_template,user_id)
    notification_template.user_notifications.build.tap do |user_notification|
      user_notification.title = notification_template.title
      user_notification.subtitle = notification_template.subtitle
      user_notification.description = notification_template.description
      user_notification.merge_field = notification_template.merge_field
      user_notification.cta = notification_template.cta
      user_notification.cta_key = notification_template.cta_key
      user_notification.secondary_cta = notification_template.secondary_cta
      user_notification.secondary_cta_key = notification_template.secondary_cta_key
      user_notification.show_useful = notification_template.show_useful
      user_notification.category = notification_template.category
      user_notification.display_screen = notification_template.display_screen
      user_notification.sent_at = Time.current
      user_notification.user_id = user_id
      user_notification.filter_preferences = notification_template.filter_preferences
      user_notification.save
    end
  end

I have tested this for 100 users and it takes 30-40 secs. god knows how much it would take for 20k users in prod.

Accessing the model's attribute translations when displaying error

I've successfully created my he.yml to localize my model's attributes names, example:

      attributes:
       vendor:
        name: שם ספק
        counter_number: מספר חשבונית
        phone: טלפון
        address: כתובת

Now, displaying labels in forms using simple_form's f.input, displays it correctly, the translated value of each attribute.

the problem is, displaying errors after validation, using

<% @vendor.errors.each do |attribute, error| %>

|attribute| for error "counter_number" for example, is displayed: "counter_number". not the translated one at the locale file [which as i mentioned previously, configured and loaded successfully]. I appended errors in a ul.errors, as shown in this screenshot: enter image description here

Thanks in advance.

How to include a link_to block within a label_tag

I am trying to use label_tag "Categories" and to include a plus button next to the label, which is a link_to do block that has a span (the plus button).

Ruby : How to get months and days between two date?

I want find number of months and days between two dates in ruby. For example:

started_at = '2015-10-27' ended_at = '2016-07-30'

So, i want a function return '9 months and 3 days'.

Rails multiple dividers in a Ruby on Rails form Selector_tag

I have a selector within a standard ruby on rails form. The selector is for countries and the dividers with be continents. But I don't want to hard code the values of the countries in. I am returning several lists of countries (one for each continent) from the controller. And I want to put that in there instead of the hard coded array.

What I have:

HTML

  <%  grouped_options = [['North America',['USA','Mexico']],['Europe',['Denmark','Germany','France']]] %>

  <%= f.select :country_id, grouped_options_for_select(grouped_options), {:include_blank => "Please select a country"}, {:required => true, :class => "white_background"} %>

what I want:

   <%  grouped_options = [['North America',@north_america_names],['Europe',@europe_names]] %>

Edit: This option is not working and I am not sure why?

Controller

   @north_america_names = Country.select(:name, :id).where("continent = 'North America').distinct
   @europe_names = Country.select(:name, :id).where("continent = 'Europe').distinct

The controller works great, it returns a list of names and ids.

jeudi 16 juin 2016

ruby openssl CSR request failing with error: The submission failed: Error Parsing Request ASN1 bad tag value met(ASN: 267 CRYPT_E_ASN1_BADTAG)

I am trying to submit a CSR request in the following way:

require 'openssl'
require 'json'

  def public_key_info
    key_info = private_key.public_key.to_pem
    key_info = key_info.sub! '-----BEGIN PUBLIC KEY-----', '-----BEGIN CERTIFICATE REQUEST-----'
    key_info = key_info.sub! '-----END PUBLIC KEY-----', '-----END CERTIFICATE REQUEST-----'
    key_info
  end


  # "Creating a new 2048bit RSA Keypair..."
  def private_key
    @private_key = OpenSSL::PKey::RSA.new 2048
  end
   payload = { 
    "CsrData" => public_key_info,
    "certTemplate" => "MyTemplate"
  }

 encoded = JSON.generate(payload)

p "Payload is #{encoded}"

 response = RestClient::Resource.new(
   'http://myURL/GenerateCertificateUsingCsr',
 ).post encoded, :content_type => 'application/json', :accept => 'text/plain'

  response_json = JSON.parse(response.body)
  p response_json

This is the error I get:

{
    "certTemplate":"MyTemplate",
    "CsrData":"-----BEGIN CERTIFICATE REQUEST-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuWeK196VcjZZFbKyEjpj\n8I6DjHbwiMi9I10tV41OEt9Ozp+M0V6TYOKNlJTXGxNUHD0lXFJBlS2z/PLQbW/3\n6C9xRkIclve5Uq8J2NmubnR9+NOt/cjPb4EJtMlxySq5cWOqEyq4UirUEfch9HMC\nkLwJ0MPdrDatZqfIv1IvhBiKfyqWV2jds3X60NlmvyGxnrd54dO8/OqNJNmw2BP9\n3aa21asRqB7oPW2H49o2gwDxF6ZEwymAFvU4jvO+BQLRDYTm8GslHyX9kCXWnYHg\nX7gqvek/mu7KqyIB44YyOjGYkVX76El32B08ruKlc+xZ8kFWC1bMzwZNoFEBKO6D\n9QIDAQAB\n-----END CERTIFICATE REQUEST-----"


}


  {"ErrorCode"=>1005, "ErrorMessage"=>"The submission failed: Error Parsing Request  ASN1 bad tag value met. 0x8009310b (ASN: 267 CRYPT_E_ASN1_BADTAG)", "Return"=>false, "p12Data"=>nil, "certexpdate"=>nil, "serialNo"=>nil}
=> true

But if I create the CSR request from the command line:

openssl req -out mytest.csr -new -newkey rsa:2048 -nodes -keyout mytest.key

Then converted the CSR so replace new line with \n string.

Then prepare a Json payload:

{
    "certTemplate":"MyTemplate",
    "CsrData":"-----BEGIN CERTIFICATE REQUEST-----\nMIIC8zCCAdsCAQAwgZUxCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJNRDEWMBQGA1UE\nBwwNU2lsdmVyIFNwcmluZzELMAkGA1UECgwCRVMxCzAJBgNVBAsMAk1MMSAwHgYD\nVQQDDBcgbXNjbGllbnQ1MS5zYW10ZXN0LmNvbTElMCMGCSqGSIb3DQEJARYWbXNj\nbGllbnQ1MUBzYW10ZXN0LmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC\nggEBAL+X4YJ041JDVfYZr2IXHEAsBc9cbtYxuLa4FkXz+enZYj+9J4qK7zl9OJ7P\nfW29jf82oyQ83RH6XrYcFJKO9cuXgkkQaNV8X6J7sbn87hHUn8xZ1SORd+OPV/ws\nHdOuuv/kQi0S1Rz9Qn7RJiEnQqC14bp50fjJDxxYBVcU/bevlMuFzf8pKQbNfWD5\nbpHHPKpN6uKAXQa2vCqRPAHMvlxCqVHf1Lmy6xojsHGDdqYcYgwG2JB140nOpKtA\nwO9jR5wF7HmqUs/u/fV+p86IaHt6rAxo8WX0Ymu+48DanMdlBqjQ222OthnTbgmD\nbW9j16kNesriu8APSpxW6f7InhsCAwEAAaAYMBYGCSqGSIb3DQEJAjEJDAdNTF9U\nVjJHMA0GCSqGSIb3DQEBCwUAA4IBAQCOxISJbXXQqFmHTwcIP+jaYM1souuptE5l\nhrG/5T1Irz357DABfQpaZkon8dIF8QRpjCY2+b44srGtbKBbnUDAgM5e+qqZjx6X\ng7Yp7LLVW9EplvMYT83M62K9UyNFqjizgXbNIxJRsApLutLBpTpB3vIpQcZYhygf\nfJx/zmN3rD3K47SdaDd9JyD7W3tnAQ1rHEG1uS+Pm9Cq5+Wi8k+nEeGHtQnY5eps\nYqA/g86m4VR5RP0+oTvq3FC57PFqrbv+lwD9brCzjAK/c/QcyBnoxnMNbFVzwhcf\nKAF82Vl9kvwOwyD8sPN19V9ldmZpMhQ/2hsuHxRLAnlwHYhqfl/9\n-----END CERTIFICATE REQUEST-----"
}

the above CSR request works fine.

What am I doing wrong with the ruby code above?