Editing records with SQLite, DataMapper, and Sinatra - ruby

I'm in the process of learning Sinatra and DataMapper. To do so, I've been playing with a "customer database" project.
Creating and deleting records is trivial and now I'm working on editing records. So far I've managed to piece together a form in my views and a couple of routes that I thought would edit a record. Here's some code to illustrate my issue:
My edit.erb view: http://gist.github.com/308405
My edit/update routes:
get '/edit/:acct' do
#title = "Edit Client Data"
#client = HE_Backend.get(params[:acct])
erb :edit
end
post '/update/:acct' do
client = HE_Backend.get(params[:acct])
client.attributes = {
:name => params['client']['name'],
:company => params['client']['company'],
:street => params['client']['street'],
:state => params['client']['state'],
:zip => params['client']['zip'],
:phone => params['client']['phone'],
:fax => params['client']['fax'],
:website => params['client']['website'],
:order_date => params['client']['order_date'],
:payment_date => params['client']['payment_date'],
:monthly => params['client']['monthly'],
:setup => params['client']['setup'],
:details => params['client']['details'],
:notes => params['client']['notes'],
:status => params['client']['status'],
}
if client.save
redirect "/show/#{client.acct}"
else
redirect('/list')
end
end
It looks like the "client.save" portion of the route is returning false, because I'm getting redirected to "/list" each time. If I use the #update method rather than #save, DM complains about "dirty records".
Anyone have any ideas as to what I'm doing wrong or can you point me to examples for editing records in SQLite with DataMapper and Sinatra?
Thanks!

This turned out to be a validations issue. If I don't have validations in place and put data types other than what's in my model in those fields, the #save method apparently returns false.

Related

Creating new Tags

Just wondering if it is possible to create a new GIT tag using Rugged. if so, an example would be greatly appreciated.
I'm basically just trying to create/move/delete tags on commit oids.
You can see some examples in test/tag_test.rb:
#repo = sandbox_init("testrepo.git")
#tag = #repo.tags.create('annotated_tag', "5b5b025afb0b4c913b4c338a42934a3863bf3644", {
:message => "test tag message\n",
:tagger => { :name => 'Scott', :email => 'schacon#gmail.com', :time => Time.now }
})
For deletion, see test/reference_test.rb:
tag = #repo.tags["test"]
#repo.references.delete(tag)
refute #repo.references.exists?("refs/tags/test")
The OP Chris Portman points out in the comments that:
The create/delete methods are actually part of the TagCollection class.
Same with branches and the BranchCollection class.

Ruby/Watir - storing watir objects in hash?

I'm new to Ruby, and I LOVE IT.
Playing around with Watir-Webdriver.
I want to store the reference to watir objects in a hash and save it to disk, WITHOUT having first defined the #browser variable.
For example
elements = {
:home => #browser.a(:href => /home.php/),
:photo => #browser.img(:id => "photo"),
:about => #browser.a(:href => /about.php/)
}
so that further I can do something like:
el = elements
el[:home].click
el[:photo].wait_until_present
el[:about].click
obviously this works if I define #browser at the very beginning..
#browser = Watir::Browser.new
but what if I want to store the 'elements' hash as YAML in a file?
Should I store the values as quoted strings and eval them on the fly? like
elements = {
:home => "#browser.a(:href => /home.php/)",
# etc...
}
# store elements as YAML file...
# load elements from YAML file
el = YAML::load_file "elements.yml"
eval(el[:home]).click
eval(el[:photo].wait_until_present
# etc...
is there a better way to do this?
Build a Class to provide access to #browser based on your YAML config.
Modify your elements structure to include the data you need rather than code. Consider this a config hash/file of sorts for your new Class.
elements = {
:home => { :tag => "a", :select => { :href => /home.php/ } },
:photo => { :tag => "img", :select => { :id => "photo" } },
:about => { :tag => "a", :select => { :href => /about.php/ } }
}
Build a class to load the elements YAML file and provide access to what you need from #browser based on what is loaded.
class WatirYamlBrowserSelect
# To store your config and browser.
attr_accessor :elements, :browser
def initialize elements_yaml_file
#elements = YAML.load_file elements_yaml_file
end
# Retrieve a configured element from Watir Browser.
def element name
#browser.send( #elements[name][:tag], #elements[name][:select] )
end
end
Then when you need to use it
# Create an instance of your selector.
s = WatirYamlBrowserSelect.new( "elements.yaml" )
# Add the browser when you have it
s.browser #browser
# Access the #browser elements
s.element :home
s.element :photo
s.element :about
Alister Scott's blog as well as its code in github was the template I used for building all the page objects for a few projects. I think it should solve the repetition issues you described. It also solves the problem of maintaining too many variables for too many pages of objects and keeps objects organized by page and not in a more complex data structure, especially when the number of objects increases.
http://watirmelon.com/2011/06/07/removing-local-page-references-from-cucumber-steps/
http://watirmelon.com/2012/06/04/roll-your-own-page-objects/
https://github.com/alisterscott/wmf-custom-page-object

Using paperclip gem in rails3

Using paperclip gem in rails3, there are two copies of image uploaded simultaneously of which one is having null entries and the other is original in the database as I checked in localhost/phpmyadmin. This problem unnecessarily populates my database. Have been searching for quite a few days. Reviewed many answers regarding multiple images but no one mentioned about this problem.
I've followed this code https://github.com/websymphony/Rails3-Paperclip-Uploadify.
Paperclip was also uploading the actual image data into the field image in my database. I had to tweak it to save file names in the image_file_name field in my database.
Here is my controller that saves the image from the upload form.
#paperclip replaces spaces with _
formatted_filename = params[:clothe][:image].original_filename
formatted_filename.gsub!(/\s/,'_')
#hook in image processing
#set type of upImg, formUpload (APIUpload, scrapeUpload, mobileUpload)
image = UploadImage.new(formatted_filename, Rails.root.to_s + '/public/products/', #clothe.id)
image.processImage
Here is my model
class Product < ActiveRecord::Base
attr_accessible :description, :price, :title, :image, :image_file_name, :published
has_attached_file :image,
:styles => {
:thumb => "100x100#",
:small => "150x150>",
:medium => "200x200" },
:default_url => '/assets/missin.gif',
:path => Rails.root.to_s + "/public/products/:filename",
:url => "/products/published/:basename.:extension"

Issues with Carrierwave in Rails 3.1.0

I'm trying to attach a file "attachment" to my upload model. The attachment field in my db after creation is nil, and links link #upload.attachment.url just redirect to a parent object. Maybe I'm doing something wrong? I haven't used Carrierwave before.
# Model
require 'carrierwave/orm/activerecord'
class Upload < ActiveRecord::Base
mount_uploader :attachment, AttachmentUploader
end
Went with the basics for for the attachment field
# Form
= form_for #upload, :html => { :multipart => true } do |f|
%br
= f.file_field :attachment
And more basics with the controller:
def create
#upload = Upload.new(params[:upload])
#upload.attachment = params[:file]
if #upload.save
redirect_to #upload
end
end
I'm not getting any errors in my console, but the :attachment string on the student model is always nil.
Thanks!
Why u have added the line
#upload.attachment = params[:file]
remove it. it will work. attachment string is null because there is not params file in the form.

How to retrieve an object from a CouchDB view using the CouchRest Ruby Gem

Assuming I have a view in my CouchDB named "user/all" and a CouchRest ExtendedDocument as follows:
class User < CouchRest::ExtendedDocument
property :username
property :password
property :realname
property :role
property :rights
end
How would I go about retrieving a document for the key 'admin' from this view using this ExtendedDocument?
(If I need to make changes to the ExtendedDocument subclass, what should be changed?)
Many thanks.
Try this:
class User < CouchRest::ExtendedDocument
property :username
property :password
property :realname
property :role
property :rights
view_by :role
end
Here, I am assuming 'admin' is a role property. This will make a view in your design document keyed by role. Then, to get all 'admin' documents, you just do the following:
#admins = User.by_role(:key => 'admin')
If in fact the actual id of the document is 'admin', then all you have to do is this:
#admin = User.get('admin')
Or, alternatively:
#admin = User.all(:key => 'admin')
I would also suggest taking a look at CouchRest Model, which is basically an Active Model complaint extension to CouchRest if you are using this with Rails. Good Luck!

Resources