ruby framework method -> what's going on here? - ruby

def partial(template, *args)
options = args.extract_options!
options.merge!(:layout => false)
if collection = options.delete(:collection) then
collection.inject([]) do |buffer, member|
buffer << erb(template, options.merge(:layout =>
false, :locals => {template.to_sym => member}))
end.join("\n")
else
erb(template, options)
end
end
This method has no docs. It seems to be some way of letting you add additional features to partial rendering in an erb template.
How does this Ruby code work?
I don't care as much about the role this plays in a web framework. I just would like to understand what's going on in terms of Ruby syntax.

It works much like doing render :partial in Rails — it takes a partial and a list of options (e.g. a collection of objects to render using the partial) and renders the partial with those options. Except this method appears to have ERb hardcoded in. If this is from Rails, I think this must be a very old method that isn't meant for use but hasn't yet been removed (maybe for compatibility with something or another).
The options.merge!(:layout => false) is effectively like doing options[:layout] = false.
options.delete(:collection) deletes the entry for ":collection" from the options hash and returns it if it exists. If there wasn't a collection entry, it returns nil, so the associated if-block won't run. If there is a collection, it renders the partial for each element of the collection and returns the accumulated result of rendering all of them. If there is not a collection, it just renders the partial with the options specified.

To understand this, you need to understand the docs on these methods:
extract_options!
Enumerable/Array: merge, merge!, inject, join, delete
Once you understand those, there's nothing tricky about the syntax here. You should be able to read it straight through.
Something in particular?

Related

How to make sense of this Object.inspect result?

I'm new to Ruby and trying to learn the ropes. I have an object returned from a call to the Twitter API. I'd like to loop through the :users below and be able to print out their different attributes... like :id, :screenname, etc.
BTW, these are all followers on twitter. So the object is called "Followers" and this is what I'm calling to get the inspect... "followers.inspect"
The gist of the followers.inspect is here:
https://gist.github.com/anonymous/7966898/raw/938a0048d08f44415229b98a1adef96c4946ce8d/gistfile1.txt
My problem is that I'm having a hard time figuring out how this .inspect print out matches up to what I should be looking for.
So the code I've written to try to work through this is...
followers.users.each do |i|
puts "#{i.name} has user id #{i.id_str}"
end
The error I'm getting is that there is no method called users.
Sorry, I'm sure this is a very simple question but have just gotten stuck!
As far as I can quickly see, the object you have contains a hash #attrs with a key :users.
So, it should be something like:
followers.attrs[:users].each do |i|
puts "#{i.name} has user id #{i.id_str}"
end
Maybe you'll also need to use i[:name] since all those users are also hashes, not objects with methods, but my Ruby is a bit rusty, so I don't know if those methods are automatically available when a key is present in a hash.
Anyway: {:key => "value"} in inspect means that it's a hash with keys and values, that you access like: nameofhash[:key]
[{:key => "value"},{:key => "value"}] means it's a array with two seperate hashes.

How much should interfaces of elements in Page Objects be abstracted?

I have a page object called LineItemsPage
class LineItemsPage
attr_accessor :add_line_item_button
def initialize(test_env)
#browser = test_env[:browser]
#action_bar = #browser.div(:id => 'lineitems_win').div(:class => 'window-body').div(:class => 'actionbar')
#add_line_item_button = #action_bar.img(:class => 'button add')
end
def method_missing(sym, *args, &block)
#browser.send sym, *args, &block
end
end
I use it like so:
When /^I click on Add Item and enter the following values:$/ do |table|
#line_items_page = LineItemsPage.new(#test_env)
#line_items_page.add_line_item_button.when_present.click
end
I'm wondering if I should be abstracting the click, by adding something like the following to my LineItemsPage class:
def add_item
self.add_line_item_button.when_present.click
end
And then using it like so:
#line_items_page.add_item
I'm looking for best practices, either with regards to Page Object in particular or Ruby in general. I feel that encapsulating the interface by using add_item() is going a bit far, but I'm wondering if I'm unaware of issues I might run into down the road if I don't do that.
Personally, I try to make my page object methods be in the domain language with no reference to the implementation.
I used to do something like #line_items_page.add_line_item_button.when_present.click, however it has caused problems in the following scenarios:
1) The add line item was changed from a button to a link.
2) The process for adding a line item has changed - say its now done by a right-click or it has become a two step process (like open some dropdown and then click the add line).
In either case, you would have to locate all the places you add line items and update them. If you had all the logic in the add_item page object method, you would only have to update the one place.
From an implementation perspective, I have found that Cheezy's page object accessors work pretty well. However, for image buttons (or any of your app's custom controls), I would add additional methods to the PageObject::Accessors module. Or if they are one off controls, you can add the methods directly to the specific page object.
Update - Reply to Comment Regarding Some Starting Points:
I have not come across too much documentation, but here are a couple links that might help:
1) The Cheezy Page Object project wiki - Gives a simple example to get started
2) Cheezy's blog posts where the page object gem first started. Note that the content here might not be exactly how the gem is currently implemented, but I think it gives a good foundation to understanding what he is trying to achieve. This in turn makes it easier to understand what is happening when you have to open up and modify the gem to fit you needs.

PageObject with Ruby - set text in a text field only works in the main file

I'm automating a site that has a page with a list of options selected by a radio button. When selecting one of the radios, a text field and a select list are presented.
I created a file (test_contracting.rb) that is the one through which I execute the test (ruby test_contracting.rb) and some other classes to represent my page.
On my class ContractPage, I have the following element declaration:
checkbox(:option_sub_domain, :id => "option_sub_domain")
text_field(:domain, :id => "domain_text")
select_list(:tld, :id => "domain_tld")
I've created in the ContractPage a method that sets the configuration of the domain like this:
def configure_domain(config={})
check_option_sub_domain
domain = config[:domain]
tld = config[:tld]
end
When I call the method configure_domain from the test_contracting.rb, it selects the radio button, but it doesn't fill the field with the values. The params are getting into the method correctly. I've checked it using "puts". Even if I change the params to a general string like "bla" it doesnt work. The annoying point is that if on test_contracting.rb I call the exact same components, it works.
my_page_instance = ContractPage.new(browser)
my_page_instance.domain = "bla"
my_page_instance.tld = ".com"
What I found to work was to in the configure_domain method, implement the following:
domain_element.value = config[:domain]
tld_element.send_keys config[:locaweb_domain]
Then it worked.
The documentation for the PageObjects module that I'm using as reference can be found here: http://rubydoc.info/github/cheezy/page-object/master/PageObject/Accessors#select_list-instance_method
Do you guys have any explation on why the method auto generated by the pageobject to set the value of the object didnt work in this scope/context ?
By the way, a friend tried the same thing with Java and it failed as well.
In ruby all equals methods (methods that end with the = sign) need to have a receiver. Let me show you some code that will demonstrate why. Here is the code that sets a local variable to a value:
domain = "blah"
and here is the code that calls the domain= method:
domain = "blah"
In order for ruby to know that you are calling a method instead of setting a local variable you need to add a receiver. Simply change your method above to this and it will work:
def configure_domain(config={})
check_option_sub_domain
self.domain = config[:domain]
self.tld = config[:tld]
end
I'm pretty new to this world of Selenium and page objects but maybe one of my very recent discoveries might help you.
I found that that assignment methods for the select_list fields only worked for me once I started using "self" in front. This is what I have used to access it within my page object code. e.g., self.my_select_list="my select list value"
Another note - The send_keys workaround you mention is clever and might do the trick for a number of uses, but in my case the select list values are variable and may have several options starting with the same letter.
I hope something in here is useful to you.
UPDATE (Jan 3/12)
On diving further into the actual Ruby code for the page object I discovered that the select_list set is also using send_keys, so in actuality I still have the same limitation here as the one I noted using the send_keys workaround directly. sigh So much to learn, so little time!

Padrino model from json data

I have been looking at Padrino for a project I am working on, and it seems a great fit, as I would ideally be wanting to support data being sent and received as json.
However I am wondering if there is any automated helper or functionality built in to take data from a post request (or other request) and put that data into the model without having to write custom logic for each model to process the data?
In the Blog example they briefly skim over this but just seem to pass the parameter data into the initilizer of their Post model, making me assume that it just magically knows what to do with everything... Not sure if this is the case, and if so is it Padrino functionality or ActiveRecord (as thats what they seem to use in the example).
I know I can use ActiveSupport for JSON based encoding/decoding but this just gives me a raw object, and as the storage concerns for each model reside within the main model class I would need to use a mixin or something to achieve this, which seems nasty.
Are there any good patterns/functionality around doing this already?
Yep, you can use provides and each response object will call to_json i.e:
get :action, :provides => :json do
#colletion = MyCollection.all
render #collection # will call #collection.to_json
end
Here an example of an ugly code that fills certain models.
# Gemfile
gem 'json' # note that there are better and faster gems like yajl
# controller
post "/update/:model/:id", :provides => :json do
if %w(Account Post Category).include?(params[:model])
klass = params[:model].constantize
klass.find(params[:id])
klass.update_attributes(JSON.parse(params[:attributes]))
end
end
Finally if you POST a request like:
attributes = { :name => "Foo", :category_id => 2 }.to_json
http://localhost:3000/Account/12?attributes=#{attributes}
You'll be able to update record 12 of the Account Model.

Expiring all caches on a controller

I got a resourceful controller with a custom action. The action is pretty heavy, so I'm working on caching it:
class MyController < ApplicationController
caches_action :walk_to_mordor
# GET /my/:id/walk_to_mordor/:direction
def walk_to_mordor
# srz bzns
end
end
It works very nice, caching is done and the page is now fast. However, I want to allow the user to "bust" the cache by clicking on a link on the page. At first I tried:
def bust_cache
expire_action :action => :walk_to_mordor
end
Rails complained that no route matches my action. Might be because of the parameter. Hmm, let's give it to him:
def bust_cache
MyEntities.all.each do |e|
expire_action walk_to_mordor_path(e, ??)
end
end
Problem, I can't possibly identify all choices of :direction.
Is there a way to clear all action caches that match a certain regular expression, or all action caches from a specific controller?
The secret is called expire_fragment:
expire_fragment(key, options = nil)
Removes fragments from the cache.
key can take one of three forms:
String - This would normally take the form of a path, like "pages/45/notes".
Hash - Treated as an implicit call to url_for, like {:controller => "pages", :action => "notes", :id => 45}
Regexp - Will remove any fragment that matches, so %r{pages/d*/notes} might remove all notes. Make sure you don’t use anchors in the regex (^ or $) because the actual filename matched looks like ./cache/filename/path.cache. Note: Regexp expiration is only supported on caches that can iterate over all keys (unlike memcached).
http://api.rubyonrails.org/classes/ActionController/Caching/Fragments.html#method-i-expire_fragment
Sadly, it won't work with memcached (if I ever decide to use it). Gotta be a lot more clever to avoid cache in that circunstance. Maybe adding a serial parameter to the request, and increment it when the user presses the 'bust cache' button...

Resources