William Jones

Joined

8,890 Experience
11 Lessons Completed
10 Questions Solved

Activity

Posted in Modified Cyclomatic Complexity

Rubocop does something called perceived complexity which is the same as modified cyclomatic complexity so that is the solution.

Posted in Modified Cyclomatic Complexity

Do you know of a tool that does modified cyclomatic complexity analysis of Ruby code? Lizard does not do it correctly for Ruby, and I could not find a modified option on Rubocop.

Posted in I am confused about do blocks

This is all to do with Prawn so I have my answer.

Posted in I am confused about do blocks

I am familiar with do while, do until, each do, etc. However I came across the following code that is using 'do' in a way I have not seen before and I am not sure what it is 'doing'. For example

'bounding_box [bounds.left, cursor], width: bounds.width do'

How many times would the block be executed, is there some sort of counter and how is it adjusted?

Even more confusing what is this doing,

'float do'

I have copied the complete method below if it helps. Any insights are welcome. I can't find anything with google searches that helps me understand this.

def draw_summary
bounding_box [bounds.left, cursor], width: bounds.width do
text "#{@service.scheduled_assessments.count} assessments are scheduled for next week."
move_down 10

  text 'Usage'.upcase, style: :bold
  move_down 5

  image File.join(@output_dir, WeeklyTeamStatusReportService.usage_chart_file_name),
        fit: [bounds.width, WeeklyTeamStatusReportService::USAGE_CHART_HEIGHT], position: :center
  move_down 10

  text 'Fuel'.upcase, style: :bold
  move_down 5

  image File.join(@output_dir, WeeklyTeamStatusReportService.mes_chart_file_name),
        fit: [bounds.width, WeeklyTeamStatusReportService::MES_CHART_HEIGHT], position: :center
  move_down 10

  text 'Body Comp'.upcase, style: :bold
  move_down 5

  image File.join(@output_dir, WeeklyTeamStatusReportService.body_fat_status_chart_file_name),
        fit: [bounds.width, WeeklyTeamStatusReportService::BODY_FAT_STATUS_CHART_HEIGHT], position: :center
  move_down 20

  if @service.call_out_athletes.count > 0
    text "#{@team.orgtype.t('athlete', count: @service.call_out_athletes.count, plural: true)} to watch (included on the following pages):"
    move_down 5
    @service.call_out_athletes.first(8).each_slice(3).with_index.each do |athletes, slice_idx|
      bounding_box([bounds.left + (bounds.width / 3) * slice_idx, cursor], width: bounds.width / 3) do
        float do
          athletes.each.with_index do |athlete, athletes_idx|
            text "#{athlete.name}", size: 10
            move_down 5
            if slice_idx == 2 && athletes_idx == athletes.length - 1 && @service.call_out_athletes.count > 8
              text "+ #{@service.call_out_athletes.count - 8} more", size: 10
            end
          end
        end
      end
    end
  else
    text "Great news! None of the #{@team.orgtype.t('athlete', plural: true)} scanned this period have low muscle energy levels."
  end
end

end

Posted in Setup MacOS 11 Big Sur Discussion

When I run ruby -v I get the response ruby 2.6.3 instead of ruby 3.0.2. What do you think the problem might be?

I am following the tutorial for installing Ruby on Rails on MacOS and I have run the commands rbenv install 3.0.2, rbenv local 3.0.2 and rbenv global 3.0.2. But when I run the command ruby -v I get ruby 2.6.3p62. I was expecting ruby 3.0.2. What have I misunderstood or what might I have done wrong?

I am trying to install the ruby_opencv gem but it can't find opencv_contrib. I used the command 'gem install ruby-opencv -- --with-opencv-dir=/mnt/c/Users/wjones/sites/musclesound-rails/opencv/my_build_dir' which is where is loaded opencv. I have done some google searches but I can't find anything that helps. I am using Ruby 2.5.3 and Rails 5.2.4.3. The mkmf.log contains the following,

have_library: checking for -lopencv_contrib... -------------------- no

"gcc -o conftest -I/home/wjones/.rbenv/versions/2.5.3/include/ruby-2.5.0/x86_64-linux -I/home/wjones/.rbenv/versions/2.5.3/include/ruby-2.5.0/ruby/backward -I/home/wjones/.rbenv/versions/2.5.3/include/ruby-2.5.0 -I. -I/usr/include -I/mnt/c/Users/wjones/sites/musclesound-rails/opencv/my_build_dir/include -I/home/wjones/.rbenv/versions/2.5.3/include -O3 -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wno-tautological-compare -Wno-parentheses-equality -Wno-constant-logical-operand -Wno-self-assign -Wunused-variable -Wimplicit-int -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wmisleading-indentation -Wno-packed-bitfield-compat -Wsuggest-attribute=noreturn -Wsuggest-attribute=format -Wimplicit-fallthrough=0 -Wduplicated-cond -Wrestrict -fPIC conftest.c -L. -L/home/wjones/.rbenv/versions/2.5.3/lib -Wl,-rpath,/home/wjones/.rbenv/versions/2.5.3/lib -L/usr/lib -Wl,-rpath,/usr/lib -L/mnt/c/Users/wjones/sites/musclesound-rails/opencv/my_build_dir/lib -Wl,-rpath,/mnt/c/Users/wjones/sites/musclesound-rails/opencv/my_build_dir/lib -L. -L/home/wjones/.rbenv/versions/2.5.3/lib -fstack-protector -rdynamic -Wl,-export-dynamic -lopencv_calib3d -lstdc++ -Wl,-rpath,/home/wjones/.rbenv/versions/2.5.3/lib -L/home/wjones/.rbenv/versions/2.5.3/lib -lruby -lopencv_contrib -lopencv_calib3d -lstdc++ -lpthread -ldl -lcrypt -lm -lc"
/usr/bin/ld: cannot find -lopencv_contrib
collect2: error: ld returned 1 exit status
checked program was:
/* begin /
1: #include "ruby.h"
2:
3: /*top
/
4: extern int t(void);
5: int main(int argc, char *argv)
6: {
7: if (argc > 1000000) {
8: printf("%p", &t);
9: }
10:
11: return 0;
12: }
13:
14: int t(void) { ; return 0; }
/
end */


The output from the install is as follows,

wjones@WJONES-LT:/mnt/c/Users/wjones/sites/musclesound-rails$ gem install ruby-opencv -- --with-opencv-dir=/mnt/c/Users/wjones/sites/musclesound-rails/opencv/my_build_dir
Building native extensions with: '--with-opencv-dir=/mnt/c/Users/wjones/sites/musclesound-rails/opencv/my_build_dir'
This could take a while...
/home/wjones/.rbenv/versions/2.5.3/lib/ruby/2.5.0/rubygems/ext/builder.rb:76: warning: Insecure world writable dir /mnt/c in PATH, mode 040777
ERROR: Error installing ruby-opencv:
ERROR: Failed to build gem native extension.

current directory: /home/wjones/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/ruby-opencv-0.0.18/ext/opencv

/home/wjones/.rbenv/versions/2.5.3/bin/ruby -r ./siteconf20210831-1654-prpzjm.rb extconf.rb --with-opencv-dir=/mnt/c/Users/wjones/sites/musclesound-rails/opencv/my_build_dir

Check the required libraries...
checking for -lstdc++... yes
checking for -lopencv_calib3d... yes
checking for -lopencv_contrib... no
*** extconf.rb failed ***

Any help you could give would be great.

I have 2 URL's to access the same Rails application. One URL is internal to our network and does not require authentication. The other is accessible outside our network and does require authentication. I put the following code in the app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
before_action :authenticate_user!, if: :outside_the_network_is_true?

private

def outside_the_network_is_true?
logger.info 'request.original_url[7..9] = ' + request.original_url[7..9]
outside_the_network = false
if request.original_url[7..9] == 'ops'
outside_the_network = true
end
outside_the_network
end

end

This forces the user to sign in when using the external URL but allows them access without signing in when using the internal URL. This all works as expected. However I have some JavaScript that periodically requests data from the API in the same application. If the request comes from the external URL page everything is fine, but if it comes from the internal URL page I get a 401 unauthorized error. I am assuming that Devise is doing something to check if the request is coming from a signed in user and rejecting it because the user is not signed in. But I could be wrong. How should I get around this?
Any help would be much appreciated.

Posted in Stimulus Reflex error

I am getting the an error when following the 'Introduction To Stimulus Reflex video'. What would cause this and what are possible solutions?

Uncaught Error: Cannot find module './application_controller'
at webpackMissingModule (index.js:1)

I had problems because I didn't follow the instructions exactly. Everything works now.

I have been following the instructions at https://gorails.com/setup/ubuntu/20.04#ruby-rbenv. I get the following error,

Installing nokogiri 1.10.9 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory: /var/lib/gems/2.7.0/gems/nokogiri-1.10.9/ext/nokogiri

/usr/bin/ruby2.7 -I /usr/lib/ruby/2.7.0 -r ./siteconf20200522-63887-8bdogk.rb extconf.rb
mkmf.rb can't find header files for ruby at /usr/lib/ruby/include/ruby.

The rails version installed is 6.0.3.1, the ruby version is 2.7 and the Ubuntu version is 20.04 LTS. I am confused about the rails version because I used the 'gem install rails -v 6.0.2.2' command.

Any advice about how to proceed would be welcome.

Posted in Prawn disposition: "inline" not working

I deleted all the old pallet data, created some new data and that displayed inline without any problem.

Posted in Prawn disposition: "inline" not working

I have been following the Railscast video #153 PDFs with prawn. I want the PDF to display inline instead of down loading it, the following is my code;

def show
respond_to do |format|
format.html
format.pdf do
pdf = Prawn::Document.new
pdf.text "Origin: "+@pallet.origin_cc
send_data pdf.render, filename: "Pallet #{@pallet.id}.pdf",
type: "application/pdf",
disposition: "inline"
end
end
end

I am using rails 5.1.7, ruby 2.5.3 and prawn 2.2.2.

However the file is not shown inline but is still down loaded. What could be the cause of this?

I continued with my google searches and found a suggestion that worked. I just ran 'gem update' and then 'rails new pallet_tracker' and everything worked.

I have created many rails projects to date without any problems. However when I tried to create a new project today using the command 'rails new pallet_tracker' I get the following error,

/home/billj/.rbenv/versions/2.5.3/lib/ruby/site_ruby/2.5.0/rubygems/specification.rb:2302:in `raise_if_conflicts': Unable to activate concurrent-ruby-ext-1.1.5, because concurrent-ruby-1.1.6 conflicts with concurrent-ruby (= 1.1.5) (Gem::ConflictError)

I get the same error when I enter the 'rails -v' command.

I am not sure how to resolve the conflict. There is no project so I can't change the gem file. Perhaps I have an outdated version of something but I don't know what that is. Any pointers will be gratefully received.

I changed the sort in the view as follows and everything works.

<%= form.fields_for :prospect_calls, form.object.prospect_calls.sort_by(&:call_date) do |p| %>

Sorry for the confusing question, I couldn't think of a better way to phrase it.

I have a 'Prospect' model that accepts nested attributes for 'prospect_calls'. When editing the prospect I need to see all the prospect calls in the order that they were made and be able to add a new prospect call if required.

In the controller I have the following code,

@prospect.prospect_calls.build call_date: @required_date

This adds a new call record that can be edited or ignored by the user. In the prospect model I reject prospect calls if the "who" field on the call record is not entered. This all works perfectly if the calls are not sorted. If they are sorted the new call record does not appear in the call list on the edit view.

To sort the calls I have included the following in the view,

<%= form.fields_for :prospect_calls, form.object.prospect_calls.order(:call_date) do |p| %>

This sorts the calls perfectly, but the new call record does not appear in the list of calls.

How can I sort the call records and have a new call record on the edit view?

Posted in How do I style select options?

Thanks

Posted in How do I style select options?

I have options for a select field from 2 sources (this years options and last years options). In the drop down list I would like to distinguish the options between the sources (e.g. this years options with a green background, last years with a pink background).

Posted in How do I drag and drop an image file?

Thanks.

logo Created with Sketch.

Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

© 2022 GoRails, LLC. All rights reserved.