jeudi 13 août 2015

Sprockets unable to locate stylesheets in a gem

Sprockets is unable to find stylesheets and javascript bundled in a gem. It raises the exception couldn't find file 'fullcalendar' with type 'text/css'

My application.css includes *= require fullcalendar and the result of running Rails.application.config.assets.paths includes "/usr/local/rvm/gems/ruby-2.2.1/gems/fullcalendar-rails-2.3.1.0/vendor/assets/stylesheets"

From what I understand, require should be looking in the vendor folder for stylesheets named fullcalendar, but it doesn't seem to be.

Are there any configurations I need to make before Sprockets will check this other folder?



via Chebli Mohamed

Datetime fails to save but without error

I try saving a row that has an attribute named class_datetime (which is a DateTime) and it silently fails. Here's an example:

I create a new LittleClassSession, like so:

irb(main):010:0> l = LittleClassSession.new
=> #<LittleClassSession id: nil, length: nil, class_datetime: nil, created_at: nil, updated_at: nil, little_class_id: nil, location_id: nil, session_series_token: nil>

The class_datetime is a DateTime:

irb(main):011:0> LittleClassSession.columns_hash["class_datetime"].type
=> :datetime

I set the attribute:

irb(main):012:0> l.class_datetime = DateTime.now
=> 2015-08-13 22:02:09 -0400

And I save (no validations yet):

irb(main):013:0> l.save
   (0.1ms)  begin transaction
  SQL (0.4ms)  INSERT INTO "little_class_sessions" ("class_datetime", "created_at", "updated_at") VALUES (?, ?, ?)  [["class_datetime", "2015-08-13 22:02:09.976030"], ["created_at", "2015-08-13 22:03:01.255911"], ["updated_at", "2015-08-13 22:03:01.255911"]]
   (2.6ms)  commit transaction
=> true

Looks like it worked. Let's see what I saved:

irb(main):014:0> l
=> #<LittleClassSession id: 728, length: nil, class_datetime: "2015-08-14 02:02:09", created_at: "2015-08-14 02:03:01", updated_at: "2015-08-14 02:03:01", little_class_id: nil, location_id: nil, session_series_token: nil>

Let's see what's in the table:

irb(main):015:0> LittleClassSession.last
  LittleClassSession Load (0.2ms)  SELECT  "little_class_sessions".* FROM "little_class_sessions"   ORDER BY "little_class_sessions"."id" DESC LIMIT 1
=> #<LittleClassSession id: 728, length: nil, class_datetime: nil, created_at: nil, updated_at: nil, little_class_id: nil, location_id: nil, session_series_token: nil>

What could be causing this? Recently I changed the default timezone in application.rb, but I don't see why that might be the reason.



via Chebli Mohamed

What's the proper way to run Ruby scripts in parallel?

Current I have the following code to run some ruby scripts, within a ruby script:

def run(base_directory, run_count)
  working_directory = base_directory.gsub("\n","")
  for i in 1..run_count
    system("ruby " + working_directory + i.to_s + "\\" + "main.rb " +   working_directory + i.to_s + "\\")
  end
end

However this runs the scripts in a sequence, but I need them to run in parallel. Where I have 10 scripts to run, and I want to run 5 at a time until I reach the number of scripts that needs run. Is there a simple way to accomplish this?



via Chebli Mohamed

Active Admin sort by 2nd level Association

I have three Models. Both makes and models table have name column inside.

class Review < ActiveRecord::Base
    belongs_to :model
end

class Model < ActiveRecord::Base
    belongs_to :make
    has_many :reviews
end

class Make < ActiveRecord::Base
    has_many :models
end 

In Active Admin on Index Page I want to sort reviews by make and model. I figured out how to sort it by model, because it has key the reviews table. Tried a lot of things. Nothing works to sort by make name. I was able to list though.

column "Model", sortable: 'model_id' do |p|
    p.model.name
end
column "Make", sortable: 'make_id' do |review|
    review.model.make.name
end

Only sorting by model name works. I think if I add make_id to Reviews it will be working, but it seems redundant, cause a chain like review.model.make perfectly works



via Chebli Mohamed

can't write unknown attribute `info' PrawnPDF

I'm using Prawn PDF to generate an invoice PDF on the fly and I keep getting the error "can't write unknown attribute 'info'" but I don't have an attribute 'info' anywhere in any of the concerned database tables (or my entire database for that matter). I don't know if it's something in the gem that I'm missing. Here is the tutorial I'm following: http://ift.tt/1hxJaWh and http://ift.tt/1fahQW9

I did register the mime type in the initializer (didn't show bc it's so simple, but I can if that's needed)

I also restarted the server multiple times to make sure that wasn't the issue.

I know the gem is installed properly because I can generate an empty PDF no problem, but when I try to add in @invoice is when I run into the issue.

Invoice Show Controller Action:

def show
    @invoice = Invoice.find(params[:id])
    @customer = @invoice.customer
    @user = current_user
    @company = @user.company

    respond_to do |format|
        format.html
        format.pdf do
            pdf = InvoicePdf.new(@invoice)
            send_data pdf.render, filename: "invoice_#{@invoice.created_at.strftime("%m/%d/%Y")}.pdf", type: "application/pdf"
        end
    end
end

InvoicePdf.rb

class InvoicePdf < Prawn::Document

    def intialize(invoice)
        super()
        @invoice = invoice
    end

    def header
        image "http://ift.tt/1cjbqFH", width: 530, height: 150
    end

    def text_content
        y_position = cursor - 50

        bounding_box([0, y_position], :width => 270, :height => 300) do
          text "Lorem ipsum", size: 15, style: :bold
          text "Lorem ipsum dolor sit amet, consectetur adipiscing elit.  Suspendisse interdum semper placerat. Aenean mattis fringilla risus ut fermentum. Fusce posuere dictum venenatis. Aliquam id tincidunt ante, eu pretium eros. Sed eget risus a nisl aliquet scelerisque sit amet id nisi. Praesent porta molestie ipsum, ac commodo erat hendrerit nec. Nullam interdum ipsum a quam euismod, at consequat libero bibendum. Nam at nulla fermentum, congue lectus ut, pulvinar nisl. Curabitur consectetur quis libero id laoreet. Fusce dictum metus et orci pretium, vel imperdiet est viverra. Morbi vitae libero in tortor mattis commodo. Ut sodales libero erat, at gravida enim rhoncus ut."
        end

        bounding_box([300, y_position], :width => 270, :height => 300) do
          text "Duis vel", size: 15, style: :bold
          text "Duis vel tortor elementum, ultrices tortor vel, accumsan dui. Nullam in dolor rutrum, gravida turpis eu, vestibulum lectus. Pellentesque aliquet dignissim justo ut fringilla. Interdum et malesuada fames ac ante ipsum primis in faucibus. Ut venenatis massa non eros venenatis aliquet. Suspendisse potenti. Mauris sed tincidunt mauris, et vulputate risus. Aliquam eget nibh at erat dignissim aliquam non et risus. Fusce mattis neque id diam pulvinar, fermentum luctus enim porttitor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos."
        end
    end

end

Full Trace:

    activerecord (4.1.0) lib/active_record/attribute_methods/write.rb:72:in `write_attribute'
    activerecord (4.1.0) lib/active_record/attribute_methods/dirty.rb:68:in `write_attribute'
    activerecord (4.1.0) lib/active_record/attribute_methods.rb:390:in `[]='
    pdf-core (0.6.0) lib/pdf/core/document_state.rb:42:in `normalize_metadata'
    pdf-core (0.6.0) lib/pdf/core/document_state.rb:5:in `initialize'
    prawn (2.0.2) lib/prawn/document.rb:200:in `new'
    prawn (2.0.2) lib/prawn/document.rb:200:in `initialize'
    app/controllers/invoices_controller.rb:23:in `new'
    app/controllers/invoices_controller.rb:23:in `block (2 levels) in show'
    actionpack (4.1.0) lib/action_controller/metal/mime_responds.rb:258:in `call'
    actionpack (4.1.0) lib/action_controller/metal/mime_responds.rb:258:in `respond_to'
    app/controllers/invoices_controller.rb:20:in `show'
    actionpack (4.1.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
    actionpack (4.1.0) lib/abstract_controller/base.rb:189:in `process_action'
    actionpack (4.1.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
    actionpack (4.1.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
    activesupport (4.1.0) lib/active_support/callbacks.rb:113:in `call'
    activesupport (4.1.0) lib/active_support/callbacks.rb:113:in `call'
    activesupport (4.1.0) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
    activesupport (4.1.0) lib/active_support/callbacks.rb:149:in `call'
    activesupport (4.1.0) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
    activesupport (4.1.0) lib/active_support/callbacks.rb:166:in `call'
    activesupport (4.1.0) lib/active_support/callbacks.rb:166:in `block in halting'
    activesupport (4.1.0) lib/active_support/callbacks.rb:149:in `call'
    activesupport (4.1.0) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
    activesupport (4.1.0) lib/active_support/callbacks.rb:229:in `call'
    activesupport (4.1.0) lib/active_support/callbacks.rb:229:in `block in halting'
    activesupport (4.1.0) lib/active_support/callbacks.rb:166:in `call'
    activesupport (4.1.0) lib/active_support/callbacks.rb:166:in `block in halting'
    activesupport (4.1.0) lib/active_support/callbacks.rb:86:in `call'
    activesupport (4.1.0) lib/active_support/callbacks.rb:86:in `run_callbacks'
    actionpack (4.1.0) lib/abstract_controller/callbacks.rb:19:in `process_action'
    actionpack (4.1.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
    actionpack (4.1.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
    activesupport (4.1.0) lib/active_support/notifications.rb:159:in `block in instrument'
    activesupport (4.1.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    activesupport (4.1.0) lib/active_support/notifications.rb:159:in `instrument'
    actionpack (4.1.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
    actionpack (4.1.0) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
    activerecord (4.1.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
    actionpack (4.1.0) lib/abstract_controller/base.rb:136:in `process'
    actionview (4.1.0) lib/action_view/rendering.rb:30:in `process'
    actionpack (4.1.0) lib/action_controller/metal.rb:195:in `dispatch'
    actionpack (4.1.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
    actionpack (4.1.0) lib/action_controller/metal.rb:231:in `block in action'
    actionpack (4.1.0) lib/action_dispatch/routing/route_set.rb:80:in `call'
    actionpack (4.1.0) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
    actionpack (4.1.0) lib/action_dispatch/routing/route_set.rb:48:in `call'
    actionpack (4.1.0) lib/action_dispatch/journey/router.rb:71:in `block in call'
    actionpack (4.1.0) lib/action_dispatch/journey/router.rb:59:in `each'
    actionpack (4.1.0) lib/action_dispatch/journey/router.rb:59:in `call'
    actionpack (4.1.0) lib/action_dispatch/routing/route_set.rb:676:in `call'
    omniauth (1.2.2) lib/omniauth/strategy.rb:186:in `call!'
    omniauth (1.2.2) lib/omniauth/strategy.rb:164:in `call'
    warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
    warden (1.2.3) lib/warden/manager.rb:34:in `catch'
    warden (1.2.3) lib/warden/manager.rb:34:in `call'
    rack (1.5.2) lib/rack/etag.rb:23:in `call'
    rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
    rack (1.5.2) lib/rack/head.rb:11:in `call'
    actionpack (4.1.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
    actionpack (4.1.0) lib/action_dispatch/middleware/flash.rb:254:in `call'
    rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
    rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
    actionpack (4.1.0) lib/action_dispatch/middleware/cookies.rb:560:in `call'
    activerecord (4.1.0) lib/active_record/query_cache.rb:36:in `call'
    activerecord (4.1.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
    activerecord (4.1.0) lib/active_record/migration.rb:380:in `call'
    actionpack (4.1.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
    activesupport (4.1.0) lib/active_support/callbacks.rb:82:in `run_callbacks'
    actionpack (4.1.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
    actionpack (4.1.0) lib/action_dispatch/middleware/reloader.rb:73:in `call'
    actionpack (4.1.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
    better_errors (2.1.1) lib/better_errors/middleware.rb:59:in `call'
    actionpack (4.1.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
    actionpack (4.1.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
    railties (4.1.0) lib/rails/rack/logger.rb:38:in `call_app'
    railties (4.1.0) lib/rails/rack/logger.rb:20:in `block in call'
    activesupport (4.1.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
    activesupport (4.1.0) lib/active_support/tagged_logging.rb:26:in `tagged'
    activesupport (4.1.0) lib/active_support/tagged_logging.rb:68:in `tagged'
    railties (4.1.0) lib/rails/rack/logger.rb:20:in `call'
    actionpack (4.1.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
    rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
    rack (1.5.2) lib/rack/runtime.rb:17:in `call'
    activesupport (4.1.0) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
    rack (1.5.2) lib/rack/lock.rb:17:in `call'
    actionpack (4.1.0) lib/action_dispatch/middleware/static.rb:64:in `call'
    rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
    railties (4.1.0) lib/rails/engine.rb:514:in `call'
    railties (4.1.0) lib/rails/application.rb:144:in `call'
    rack (1.5.2) lib/rack/lock.rb:17:in `call'
    rack (1.5.2) lib/rack/content_length.rb:14:in `call'
    rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
    /home/action/.parts/packages/ruby2.1/2.1.1/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
    /home/action/.parts/packages/ruby2.1/2.1.1/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
    /home/action/.parts/packages/ruby2.1/2.1.1/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'



via Chebli Mohamed

Create an array of ids with rails

I am trying to create an array of ids so that I can query if a certain id is associated with an object. Here is my method but it isnt adding id's to the array.

class Report
  include Mongoid::Document

  has_and_belongs_to_many :reportapprovals, class_name: "Reportapproval", inverse_of: :report

  def bind_reportapproval
    @reportapprovals = Reportapproval.where(tenant_id: self.tenant_id).all

    if @reportapprovals.present? && @reportapprovals.any? { |ra| ra.tenant_approved == true }
      @reportapprovals.each do |ra|
        self.reportapproval_ids = ra.id
      end
    end
  end
end

This is suppose to add an array of reportapproval_ids to the report object.



via Chebli Mohamed

Can this raw SQL be written using the Rails Active Record Query Interface? Should it be?

In my Rails 4 app I make a fairly simple search for one of my models using the following SQL 'OR' statements. It works fine. Is there any way (and reason) to achieve this without raw SQL using the Rails Active Record Query Interface?

Activity.where("
    user_id = ? OR 
    category = ? OR 
    (secondary_id = ? AND secondary_model = ?) OR 
    (tertiary_id = ? AND tertiary_model = ?)",
    user_id, "Announcement", user_id, "user", user_id, "user"
).uniq



via Chebli Mohamed