Showing posts with label Rails. Show all posts
Showing posts with label Rails. Show all posts

Saturday, November 2, 2013

getting comfortable with prawn

deployment - Capistrano for Java? - Stack Overflow

http://stackoverflow.com/questions/183091/capistrano-for-java

Deployment strategy for Java web services?

Other options include

  • ControlTier
  • Fabric (Python)
  • Func

At my work we use Capistrano exclusively to deploy all of our Java applications. It is definitely possible.

Bob Smith, http://stackoverflow.com/questions/183091/capistrano-for-java

java - Debugging in Maven? - Stack Overflow

http://stackoverflow.com/questions/2935375/debugging-in-maven

It sure would be nice to not have a dependency on Eclipse. Having access to a command-line debugger would help in that area.

mvn exec:exec -Dexec.executable="java" -Dexec.args="-classpath %classpath -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044 com.mycompany.app.App"

ruby - Rails/Prawn: how do I use rails helpers inside a Prawn class? - Stack Overflow

http://stackoverflow.com/questions/9708884/rails-prawn-how-do-i-use-rails-helpers-inside-a-prawn-class

Extending Prawn helpers was as easy as creating an initializer and putting something like this in.

# $RAILS_ROOT/config/initializers/prawnto.rb
 
 module MyFancyModule

    def party!
      text "It's a big party!"
    end

  end

  Prawn::Document.extensions << MyFancyModule

It's also possible to mix in some of your own Rails helpers or anything really. I'm not sure if this is anything other than a more formal way of introducing a monkey patch, though.

# $RAILS_ROOT/config/initializers/prawnto.rb

Prawn::Document.extensions << ReportPdf
Prawn::Document.extensions << EntriesHelper

prawnto_2 does not accept a way to use a different class for the instance. You have to inject your modifications into Prawn::Document when Rails first comes up (e.g., an initializer).

# prawnto_2-0.2.5/lib/prawnto/template_handlers/renderer.rb

      def initialize(view_context, calling_object = nil)
        @view_context = view_context
        @calling_object = calling_object
        set_instance_variables
        @pdf = Prawn::Document.new(@prawnto_options[:prawn]);
      end

Current Cursor Position when Using the Prawn Ruby Library - Stack Overflow

http://stackoverflow.com/questions/183039/current-cursor-position-when-using-the-prawn-ruby-library

#move_cursor_to is probably a better way to accomplish moving the cursor to a particular 'y' position.

move_cursor_to(200)

ruby on rails - prawnto displaying tables that don't break when new page - Stack Overflow

http://stackoverflow.com/questions/2081635/prawnto-displaying-tables-that-dont-break-when-new-page

When paginating a PDF file using Prawn, there is no other way to determine the ultimate height of a stretchy box than to render it and access the @height attribute to get its value.

It appears that programatically determining where to introduce a page break might be challenging, especially when using prawnto_2. It might just mean that it will be necessary to not use the gem that helps integrate Prawn with Rails and use more explicit notation in the controller actions.

# controller action

  respond_to do |format|
    format.html
    format.pdf do
      pdf = Prawn::Document.new
      pdf.text "This is an audit."
      # Use whatever prawn methods you need on the pdf object to generate the PDF file right here.

      send_data pdf.render, type: "application/pdf", disposition: "inline"
      # send_data renders the pdf on the client side rather than saving it on the server filesystem.
      # Inline disposition renders it in the browser rather than making it a file download.
    end
  end

There is an interesting solution for pagination that involves using transaction/rollback, but apparently it is a little buggy.

@current_page = pdf.page_count

@roll = pdf.transaction do 
  pdf.move_down 20

  pdf.table @data,
    :font_size  => 12, 
    :border_style => :grid,
    :horizontal_padding => 10,
    :vertical_padding   => 3,
    :border_width       => 2,
    :position           => :left,
    :row_colors => ["FFFFFF","DDDDDD"]

  pdf.rollback if pdf.page_count > @current_page

end 

if @roll == false

  pdf.start_new_page

  pdf.table @data,
    :font_size  => 12, 
    :border_style => :grid,
    :horizontal_padding => 10,
    :vertical_padding   => 3,
    :border_width       => 2,
    :position           => :left,
    :row_colors => ["FFFFFF","DDDDDD"]
end

#153 PDFs with Prawn (revised) - RailsCasts

http://railscasts.com/episodes/153-pdfs-with-prawn-revised?view=comments

Several comments hint at some of the cool things that can be done. A more comprehensive list of examples can be found in Prawn's self-generated help document.

ruby on rails - Using lists in prawn - Stack Overflow

http://stackoverflow.com/questions/10513581/using-lists-in-prawn

Creating a bulleted list in Prawn. It's suggested that WickedPDF offers a better PDF generating solution.

table([ ["•", "First Element"],
        ["•", "Second Element"],
        ["•", "Third Element"] ])

Referring to selected text in a zen coding operation

http://code.google.com/p/zen-coding/wiki/ZenHTMLSelectorsEn

Yes! This is a great way to take a list of urls and format them in an unordered list.

ul>li*>a[href='$#']{$#}

Tuesday, September 3, 2013

weblog; Rails views; associations and ajax

Dynamic creation of variables in ruby

    inst_var_name = params[:controller].gsub(/\//, "_").singularize
    instance_variable_set("@#{inst_var_name}", @model)
http://www.funonrails.com/2010/02/dynamic-creation-of-variables-in-ruby.html

How to use dynamic attributes / columns in Squeel statements?

Interesting and has possibilities, but it was taking too much time to figure out a generic approach, so I fell back to typical active record call.

    klass = guess_model_class
    foreign_key_id = "#{klass.reflect_on_all_associations.first.name.to_s}_id"
    @models = klass.find(:all, conditions: ["#{foreign_key_id} = ?", params[foreign_key_id.to_sym]])

Squeel demands that an non-existent variable or method be called so that #method_missing gets called and uses the undefined name to generate sql.

    klass = guess_model_class
    foreign_key_id = "#{klass.reflect_on_all_associations.first.name.to_s}_id"

    # works, but is not generic
    @models = klass.where{new_and_returning_member_progress_id == params[:new_and_returning_member_progress_id]}

    # doesn't work
    @models = klass.where{klass.columns[0] == params[foreign_key_id.to_sym]}

    # doesn't work
    @models = klass.where{table_name.send(foreign_key_id.to_sym) == params[foreign_key_id.to_sym]}
http://stackoverflow.com/questions/12612889/how-to-use-dynamic-attributes-columns-in-squeel-statements

Test if a word is singular or plural in Ruby on Rails

def test_singularity(str)
  str.pluralize != str and str.singularize == str
end
    inst_var_name = params[:controller].gsub(/\//, "_").singularize
http://stackoverflow.com/questions/1801698/test-if-a-word-is-singular-or-plural-in-ruby-on-rails

polymorphic_url: Universal partial

    <div class="well"><%= link_to "Parents", polymorphic_url([@new_and_returning_member_progress, :family, :parents], layout: "none") %></div>
http://apidock.com/rails/ActionController/PolymorphicRoutes/polymorphic_url