mercredi 5 juin 2019

Problem with Value That change for some reason

Just run this code in console. Problem is @notes_before change value after calling method "a.move" for some reasons. How to fix it and why it happen ?

class Slider
  attr_accessor :position
  def initialize(position)
    @position = position
    @notes_before = Array.new
  end

  def move
    @position.each do |note|
      note[0] +=1
    end
    print @notes_before
  end

  def update_pos
    @position.each do |notes|
      @notes_before << notes
    end
    print @notes_before
  end
end

a=Slider.new([[0,0],[0,1]])
a.update_pos
a.move

I expect output of @notes_before to be [[0, 0], [0, 1]] after calling a.move but the actual output is [[1, 0], [1, 1]]

How to click a (apparently) form-less button/ link using mechanize to 'Show More' on web page

I would like to load more data onto the page I'm visiting by clicking the 'Show More' button, which is at the bottom of the page.

Mechanize is showing me this button as a link (see below).

From the research I've done, I should be able to click this button with the code listed below. However, when I view the list of divs that I'm hoping will be longer, I see the count is the same as before, which to me means something isn't working as expected.

On another thread, I saw that buttons can sometimes be 'disguised' as part of forms. I looked at the page and it has three forms. However I do not believe any of these forms are associated with this button because the button does not have any fields associated with it.

When I do invoke the click method below, it does look like the click was successful (I can see some sort of reload in terminal) but .children.count does not change.

#assume I already did mechanize.get(url) and I'm on the site.
#I also did a mechanize.page.links.find to get the show more button

mechanize = Mechanize.new 
showMore = #<Mechanize::Page::Link "Show More" "#">
pageWithMore = mechanize.click(showMore)

# check children of div to see if count is longer
mechanize.page.css('div.results-container').children.count
# repeatedly returns 51 after 'click' invoke 

I expect the .children.count to be closer to 100 after invoking the click method and then ~150 after invoking a second time.

rails syntax error when add column to model

I am trying to build a music app with rails where I encountered error, just wondering does anyone know what it means?

when I did rails g migration add_quantity_to_line_items quantity:integer, default: 1

rails db:migrate

which threw back rails aborted! SyntaxError: /Users/Code/Ruby/musicapp/db/migrate/20190605091214_add_quantity_to_line_items.rb:4: syntax error, unexpected tSYMBEG, expecting do or '{' or '(' add_column :line_items, :default, :string ^ /Users/mrswordsmith/Code/Ruby/musicapp/db/migrate/20190605091214_add_quantity_to_line_items.rb:5: syntax error, unexpected tINTEGER, expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END add_column :line_items, :1, :string

class AddQuantityToLineItems < ActiveRecord::Migration[5.2]
  def change
    add_column :line_items, :quantity, :integer,
    add_column :line_items, :default, :string
    add_column :line_items, :1, :string
  end
end

mardi 4 juin 2019

When is it a good idea to define a static ruby class?

Are there some governing rules around when it makes sense to define a static class as opposed to a class that is instantiated?

Still puzzling over idiomatic ruby initialization vs static class definition.

From what I've found.

Static classes seem very effective.

  1. ruby is both a functional and object orientated language. it’s very possible to write both styles. a functional style in super useful for ‘functional’ things. some pieces of our our code base are static, others are instantiated.
  2. i regularly observe classes going from static to instantiated and then very quickly bloating. “now we have all this stuff.. why not do more.“. static classes have a natural tendency to taking on multiple responsibilities.
  3. when a class transitions from static to instantiated we arrive at less unit testing confidence, given things at run time might change in regards to memory allocation / assignment (as in, someone could accidentally get in between instantiation and call).
  4. static classes are easier to read because we don’t have to juggle potential state change considerations. less mutation = less complexity.

For me. The big issues are:

  1. Classes doing more than one thing.
  2. Instability in patterns across large old ruby code bases.

To address these 2 concerns, I’m thinking a static approach (where possible) seems to put a healthy amount of pressure on the code to adhere to SOLID principles.

Static

Serializer.transform(fruit:)

Instantiated

Serializer.new(fruit:).transform

What drives you to go static in ruby?

lundi 3 juin 2019

How to segregate rake from executing production DB commands from development environment?

I just noticed that a rake drop all command can also drop tables in the production db instance. Is there a way in rails to separate rails config so that the production systems are not touched by any commands from development environments ?

Rails unknown attribute during data seeding

I have an error message - "ActiveModel::UnknownAttributeError: unknown attribute 'price' for Product." I've also run rails generate migration add_Price_to_Productbut still threw me this error while seeding. I've also run this rails g model Product name 'price:decimal{12,3}' active:booleanand threw back a migration conflict from the previous migration! I am trying to create a rails shopping cart; any help would be very much appreciated.

Thank you!

this is the schema.rb

  create_table "products", force: :cascade do |t|
    t.string "product_name"
    t.string "description"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "condition"
    t.string "name"
    t.integer "user_id"
  end

this is product.rb

class Product < ActiveRecord::Base
  has_many :order_items

  default_scope { where(active: true) }
end


Rails Active Admin menu :label is not working anymore

After updating from activeadmin 1.0.0.pre5 to 1.0.0 the menu items labels are not shown correctly.

The code below is what we have been always had and worked perfectly. We're expecting to see a menu item in the Active Admin called "MODIFICACIONS EN DIRECTE". However, now it just takes it from the ":as" parameter thus the label shown es "Arribadas" (because it also pluralize it) and ignores completely the "menu :label => "MODIFICACION..."

ActiveAdmin.register ResultatProvisional, :as => 'arribada' do
  belongs_to :cursa, :finder => :find_by_slug, parent_class: Cursa
  before_action :left_sidebar!
  menu :label => "MODIFICACIONS EN DIRECTE", :priority => 1

  navigation_menu :cursa
  config.clear_action_items!

Thanks in advance for your help,

Yeray