vendredi 28 avril 2023

How to validate an updated has-many-through collection

I am try to figure out how to properly validate a model collection whenever that collection is updated using the << collection operator. In our legacy app (Rails 3.2.1), I found that platform_collection_obj.platforms is updated using << operator

Example:

collection.platforms << new_platform

In researching how to validate that each platform in platforms collection has appropriate data, I found that I should use custom-validation function. So I tried adding each_platform_branch_id validation function to PlatformCollections Model. However, I found in testing (via rails console) that the validation was not triggered.

In reading Has-Many-Association-Collection Info, I came across this

Adds one or more objects to the collection by setting their foreign keys to the collection's primary key. Note that this operation instantly fires update SQL without waiting for the save or update call on the parent object, unless the parent object is a new record. This will also run validations and callbacks of associated object(s).

This excerpt indicates that using << to perform update on collection.platforms will not trigger the parent validations, which in this case would be the PlatformCollection:each_platform_branch_id validation function. I know that manually perforning collection.save would trigger validation, but it would be too late b/c << has already updated the DB.

So to restate the question is: How do I properly validate a has-many-through-model collection whenever that collection is updated using the << collection operator?

PlatformCollections Model

class PlatformCollection < ActiveRecord::Base
  belongs_to :tezt
  has_many :platform_collection_platforms, :order => "platform_collection_platforms.default DESC"
  has_many :platforms, :through => :platform_collection_platforms, :order => "platform_collection_platforms.default DESC"

  def each_platform_branch_id
    platforms.each do |p|
      if p.branch_id != @branch.id
        err_msg = "Invalid Platform: #{p.to_json} for PlatformCollection: #{self.to_json}. " \
                  "Expected Branch: #{@branch.id}, Actual Branch: #{p.branch_id}"
        errors.add(:platform, err_msg)
      end
    end
  end

Platforms Model

class Platform < ActiveRecord::Base
  attr_accessible :name
  belongs_to :branch
  has_many :platform_collection_platforms
  has_many :platform_collections, :through => :platform_collection_platforms

Can't set changePasswordAtNextLogin to true - Google Admin Directory V1 API Rails

I am using the google-apis-admin_directory_v1 gem for Rails. According to the documentation, if you set the field changePasswordAtNextLogin to true in the insert user method it Indicates if the user is forced to change their password at next login however, when I have created users via the API with this attribute set to true it has not asked me to change my password. Here is my method call:

google_name =
      Google::Apis::AdminDirectoryV1::UserName.new(
        given_name: 'John',
        family_name: 'Smith',
        full_name: 'John Smith'
      )
    google_user =
      Google::Apis::AdminDirectoryV1::User.new(
        password: 'password',
        primary_email: 'john.smith@example.com',
        name: google_name,
        change_password_at_next_login: true
      )

jeudi 27 avril 2023

Respond_to format in Rails 5

I am having problems getting respond_to format working in Rails 5 app in controller, is just for downloading a csv list of users, as below: def customers_graylist_export @graylisted_users = Customer.where(status_trust: 'graylist') respond_to do |format| format.csv do response.headers['Content-Type'] = 'text/csv' response.headers['Content-Disposition'] = "attachment; filename=ListaCinza.csv" end end end

And getting unknown format error on line "respond_to do |format|" the mime type is registered in mime_types initializer, and this should be straightforward, but Rails 5 is a little quirky!

Any suggestions welcome?

Many Thanks

mardi 25 avril 2023

HTTPS request for ruby

I would like to take information from another website. Therefore (maybe) I should make a request to that website (in my case a HTTP GET request) and receive the response.

How can I make this in Ruby on Rails?

If it is possible, is it a correct approach to use in my controllers?

if another website is http://www.test.com, it OK but https://www.test.com,

i got it SyntaxError ((eval):1: syntax error, unexpected '<'

^ (eval):2: syntax error, unexpected tCONSTANT, expecting end-of-input 404 Not Found ^~~): app/models/cart_payment.rb:155:in `eval'

SyntaxError ((eval):1: syntax error, unexpected '<'

^ (eval):2: syntax error, unexpected tCONSTANT, expecting end-of-input 404 Not Found ^~~): app/models/cart_payment.rb:155:in `eval'

url = URI.parse("https://test.com")

  http = Net::HTTP.new(url.host, url.port);
  request = Net::HTTP::Post.new(url)
  request["Content-Type"] = "application/json"

  request.body = JSON.dump(body)

  response = http.request(request)

  response_body = eval(response.read_body)

  message = response_body[:Response]

Not able to install ruby 2.3.1 in ubuntu 22.01 using rbenv

Not able to install ruby 2.3.1 in ubuntu 22.01 using rbenv it is giving the fllowing error response:

vidur@Vidur-PC:~/rails_apps/Connectivity-Web$ rbenv install 2.3.0
To follow progress, use 'tail -f /tmp/ruby-build.20230425154726.452387.log' or pass --verbose
Downloading openssl-1.0.2u.tar.gz...
-> https://dqw8nmjcqpjn7.cloudfront.net/ecd0c6ffb493dd06707d38b14bb4d8c2288bb7033735606569d8f90f89669d16
Installing openssl-1.0.2u...
Installed openssl-1.0.2u to /home/vidur/.rbenv/versions/2.3.0

Downloading ruby-2.3.0.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.0.tar.bz2
Installing ruby-2.3.0...

WARNING: ruby-2.3.0 is past its end of life and is now unsupported.
It no longer receives bug fixes or critical security updates.


BUILD FAILED (Ubuntu 22.10 using ruby-build 20230424)

Inspect or clean up the working tree at /tmp/ruby-build.20230425154726.452387.KXKRoa
Results logged to /tmp/ruby-build.20230425154726.452387.log

Last 10 log lines:
    /tmp/ruby-build.20230425154726.452387.KXKRoa/ruby-2.3.0/lib/rdoc/store.rb:749:in `save'
    /tmp/ruby-build.20230425154726.452387.KXKRoa/ruby-2.3.0/lib/rdoc/generator/ri.rb:27:in `generate'
    /tmp/ruby-build.20230425154726.452387.KXKRoa/ruby-2.3.0/lib/rdoc/rdoc.rb:535:in `block in generate'
    /tmp/ruby-build.20230425154726.452387.KXKRoa/ruby-2.3.0/lib/rdoc/rdoc.rb:530:in `chdir'
    /tmp/ruby-build.20230425154726.452387.KXKRoa/ruby-2.3.0/lib/rdoc/rdoc.rb:530:in `generate'
    /tmp/ruby-build.20230425154726.452387.KXKRoa/ruby-2.3.0/lib/rdoc/rdoc.rb:513:in `document'
    ./bin/rdoc:20:in `<main>'


make: *** [uncommon.mk:442: rdoc] Error 1

please suggest the possible solution thanks in advance.

samedi 22 avril 2023

How to get out of a wrong folder

I'm supposed to be in LizzyOrji123/blog in my GitHub.

Now I'm in /home/lizzy123/Elizabeth -profile-picture/blog

What command should I put in to get into the right folder?

The wrong folder is not allowing me to access .json

I want to be able to access my package.json file. Currently, it says I couldn't find it using yarn

vendredi 21 avril 2023

Ruby `.in?` mean?

I am very new to ruby, I saw a line if !'Region' .in? region. What does .in? mean? I tried searching for this in all the blogs but could not find any. Please share the relavant documentations if any.