Stephen Sizer

Joined

3,780 Experience
16 Lessons Completed
1 Question Solved

Activity

Posted in Rails Webpack caching

Found the issue! It was due to my environment file in webpacker causing the assets to not be recompiled.

Posted in Rails Webpack caching

I'm having an issue in development where I've changed my main application.js file in /javascript/packs/ and the file is being cached so having a hard time testing new changes. Does anyone else have this issue?

Thanks,
Steve

Posted in How to add an image to prawn pdf?

I'm wondering how to add an image to a prawn pdf from active storage. I've tried a number of ways and can't seem to get it to render.

Thanks in advance,
Steve

Posted in Regex getting hashtags

I'm wanting to capture all hastags from the richtext field. I thought I had a solution until I came across testing where the richtext editor put HTML entites into the markup.

/#([\w]+)/
<p>#ruby you&#39;re amazing</p>

so at the moment its getting #ruby, #39 any help would be appreciated.

thanks,
Steve

Posted in Attaching file to email Active Storage

Changed to _url and not get this error:

Unsupported argument type: ActiveStorage::Attached::One
Extracted source (around line #35):

def send_notifications
AssignmentMailer.notify_user(user).deliver_later
-- AssignmentMailer.notify_team(user, upload).deliver_later ---
end
end

Posted in Attaching file to email Active Storage

Hi Guys,

I am trying to attach a pdf to an email via active storage. Here is my code below and the error I am getting.

class Assignment < ApplicationRecord
has_one_attached :upload
belongs_to :user
belongs_to :course
belongs_to :lesson

after_save :send_notifications

def send_notifications
AssignmentMailer.notify_user(user).deliver_later
AssignmentMailer.notify_team(user, upload).deliver_later
end
end

ERROR:
No such file or directory @ rb_sysopen - /rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBFQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--889c8cda6fbfb3c827ff5bcd7a4ca7ff1594a37e/weekend.pdf

Thanks,
Steve

Posted in Searchkick Elasticsearch filtering

Does anyone know of any good tutorials showing how to add filters for searching?

Thanks in advance.

Posted in How do I query a model with a value less than

I have a Lesson model that has an order field of type integer. What i want to do is find the previous lesson.

Lesson.where("order < ?" , 8)

This causes the following error. Any help would be appreciated.

Lesson Load (1.9ms) SELECT "lessons".* FROM "lessons" WHERE (order < 8) LIMIT $1 [["LIMIT", 11]]
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: syntax error at or near "order"
LINE 1: SELECT "lessons".* FROM "lessons" WHERE (order < 8) LIMIT...
^
: SELECT "lessons".* FROM "lessons" WHERE (order < 8) LIMIT $1

Posted in How do I find the next and previous lesson?

I am currently building an LMS and am looking to add in the ability for the user to navigate to the next lesson.

In the admin back end when a teacher adds a lesson they are able to give it a position, this helps lay the lessons out. I sort on both position and then by title. So the learner can see the whole list of lessons, including topics within a lesson.

Now when the user views a lesson, I want to have the functionality for the learner to be able to click next lesson button. This will mark the current lesson as viewed and also navigate to the next lesson.

What is the best way to go about this?

I have looked and have seen the following suggestions

@next_lesson = @course.lessons.where("position > ?", @lesson.position).order(:position).first

This will work as long as every lesson has a unique position set. Sometimes this may not be the case, imagine a scenario where the teacher thinks of a lesson which would be better suited to the start of the course and he wants to add this to the beginning adding the postion of 2 this would fail as there is already a lesson with this value. Would they then need to change every lesson position value to add this position in?

Not sure what the best way to do this would be? Suggestions are welcome, especially if I have missed something very obvious.

Models for Course, Lesson and Topic are shown below, but this is just standard stuff.

class Course < ApplicationRecord
  belongs_to :author, class_name: 'User', foreign_key: 'author_id'
  has_many :lessons
end

class Lesson < ApplicationRecord
  belongs_to :author, class_name: 'User', foreign_key: 'author_id'
  belongs_to :course
  has_many :topics
end

class Topic < ApplicationRecord
  belongs_to :author, class_name: 'User', foreign_key: 'author_id'
  belongs_to :lesson
end
I'm building a forum and was wondering what's the best way to handle subscriptions? 
I have an app with Devise Users and they can sign up. Once they are signed in I want them to complete there profile before they can use the app.

Anyone done this? and whats the best way to go about it?

Thanks in advance

Posted in How do I transfer user data on delete

I was thinking of having a page before delete that would populate a list of users to transfer the data to and then on that action it would delete the user :)

Posted in How do I transfer user data on delete

To give more context:

I have a users table

Then i have the following tables that also the user_id column

Courses
Notes
Comments
Questions
Answers

What I want to be able to do is. If a member of the team leaves I want to be able to assign all courses, notes, comments, questions and answers over to another user that already exists.

Thanks,
Steve

Posted in How do I transfer user data on delete

I have a rails app with company users using devise. I want to be able to delete users and select an employee to transfer the foreign keys to.

Hope that makes sense? Thanks in advance

This is correct :)

Hi Jack,

Thanks for the reply.

that code would work for a project being 2 weeks old. Some projects may run for a year and i want to run some code every week.

To give you more context...

Every week I want to create a review for the project. So after 1 whole week a review gets created for the project manager to fill out. After 2 weeks, 3 weeks, 4 weeks ... 27 weeks and so on until the project finishes.

Thanks,
Steve

I have a project model with a start and end date.

I want to bring back all pojects that are exactly a week old, 2 weeks, 3 weeks etc

Is there a way to do this using a scope?

Thanks in advance

Posted in How to use Google Maps and Markers Discussion

Great Video!!

I would love to use this with a population density overlay.. does anyone know where I can get that data?

Hi Jacob,

Thanks for your comment. You can indeed get tenders that have been assigned to the Contractor through the method above but this is just for those assigned to them directly and not indirectly through groups they belong to.

You can do this seperately via the ContractorDetail.find(contractor).tenders_by_group, I suppose I was wondering what would be the best way to combine these?

I have a method (not yet a scope) that I was playing with on the Tender model just to see if it worked.

def all_tenders
all = self.tenders
all << self.tenders_by_group
all.uniq
end

Is this the most efficient way?

I am trying to write an internal tender application and am having trouble figuring out the best approach for writing a scope.

I have the following models

class Tender < ApplicationRecord
  belongs_to :user
  belongs_to :company_detail
  has_many :questions
  has_many :assets
  has_many :tender_groups, dependent: :destroy
  has_many :groups, through: :tender_groups
  has_many :tender_contractors, dependent: :destroy
  has_many :contractor_details, through: :tender_contractors
  has_many :bids
  has_many :contractor_details_from_groups, through: :groups, source: :contractor_details

  accepts_nested_attributes_for :tender_groups
  accepts_nested_attributes_for :tender_contractors
  accepts_nested_attributes_for :assets

  validates :title, :length_days, :summary, presence: true

  scope :live, ->{ where( "tender_end_date > ?", Time.current ) }
  scope :all_public, ->{ where( public: true ) }

end

class ContractorDetail < ApplicationRecord

  filterrific :default_filter_params => { :sorted_by => 'business_name_asc' },
    :available_filters => %w[
      sorted_by
      search_query
      with_business_type_id
    ]


  self.per_page = 10

  has_many :users
  has_many :insurances
  has_many :ratings
  has_many :issues
  belongs_to :business_type
  has_and_belongs_to_many :projects
  has_one :address, as: :addressable
  has_one :pre_qualify_info
  has_many :references
  has_many :bids
  has_many :group_contractors
  has_many :groups, through: :group_contractors
  has_many :tender_contractors
  has_many :tenders, through: :tender_contractors
  has_many :tenders_by_group, through: :groups, source: :tenders

  scope :search_query, lambda { |query|
    return nil  if query.blank?
    # condition query, parse into individual keywords
    terms = query.downcase.split(/\s+/)
    # replace "*" with "%" for wildcard searches,
    # append '%', remove duplicate '%'s
    terms = terms.map { |e|
      (e.gsub('*', '%') + '%').gsub(/%+/, '%')
    }
    # configure number of OR conditions for provision
    # of interpolation arguments. Adjust this if you
    # change the number of OR conditions.
    num_or_conditions = 2
    where(
      terms.map {
        or_clauses = [
          "LOWER(contractor_details.business_name) LIKE ?",
          "LOWER(contractor_details.contact_email) LIKE ?"
        ].join(' OR ')
        "(#{ or_clauses })"
      }.join(' AND '),
      *terms.map { |e| [e] * num_or_conditions }.flatten
    )
  }

  scope :sorted_by, lambda { |sort_option|
    # extract the sort direction from the param value.
    direction = (sort_option =~ /desc$/) ? 'desc' : 'asc'
    case sort_option.to_s
    when /^business_name_/
      # Simple sort on the name colums
      order("LOWER(contractor_details.business_name) #{ direction }")
    else
      raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }")
    end
  }

  scope :with_business_type_id, lambda { |business_type_ids|
    where(:business_type_id => [*business_type_ids])
  }

  def all_tenders
    all = self.tenders
    all << self.tenders_by_group
    all.uniq
  end

  # This method provides select options for the `sorted_by` filter select input.
  # It is called in the controller as part of `initialize_filterrific`.
  def self.options_for_sorted_by
    [
      ['Name (a-z)', 'business_name_asc'],
      ['Name (z-a)', 'business_name_desc'],
      ['Business Type (a-z)', 'business_type_name_asc']
    ]
  end

  def contact_fullname
    "#{contact_first_name} #{contact_last_name}"
  end

  end

Some information on how tenders work:

Tenders can be public viewable by all contractors
Tenders have a end date (tender_end_date) where they are no longer visible to contractors

Contractors can be added to tenders via the tender_contractors (many to many)
Contractors can also be added to groups to then allow mass assignment to a tender by groups (tender_groups)

What I am looking at doing is writing a scope to pull back all tenders assigned to a given contractor that are live (not past the tender_end_date). The should include public tenders.

Should I seperate these out into individual scopes live I have started to? Public and Live, so that these can be used on their own or chained to give me exactly what I need.

Also how would I go about writing the scope to find the tenders assigned to the contractor, would this need to be written in sql?

If more detail is needed please let me know.

Thanks in advance for taking your time to look at this. I am still learning my craft.