Posts tagged ruby
Posts tagged ruby
0 notes &
HTML5 File uploader for rails
This gem use https://github.com/blueimp/jQuery-File-Upload for upload files.
Preview:
In Gemfile:
gem “rails-uploader”
In routes:
mount Uploader::Engine => '/uploader'
Architecture to store uploaded files (cancan integration):
class Asset < ActiveRecord::Base
include Uploader::Asset
def uploader_create(params, request = nil)
ability = Ability.new(request.env['warden'].user)
if ability.can? :create, self
self.user = request.env['warden'].user
super
else
errors.add(:id, :access_denied)
end
end
def uploader_destroy(params, request = nil)
ability = Ability.new(request.env['warden'].user)
if ability.can? :delete, self
super
else
errors.add(:id, :access_denied)
end
end
end
class Picture < Asset
mount_uploader :data, PictureUploader
validates_integrity_of :data
validates_filesize_of :data, :maximum => 2.megabytes.to_i
end
For example user has one picture:
class User < ActiveRecord::Base
has_one :picture, :as => :assetable, :dependent => :destroy
fileuploads :picture
end
Find asset by foreign key or guid:
@user.fileupload_asset(:picture)
Javascripts:
//= require uploader/application
Stylesheets:
*= require uploader/application
<%= uploader_field_tag :article, :photo %>
or FormBuilder:
<%= form.uploader_field :photo %>
<%= f.input :picture, :as => :uploader %>
<%= f.input :picture, :as => :uploader %>
View on github
16 notes &
This gem provide the easy way to send and check a single sms with startmobile.com.ua service.
gem 'startmobile_sms'
StartmobileSms.setup do |config| config.login = 'login' config.password = 'password' config.out_number = 'out_number' end
# Send single sms (out_number is optional) sms_id = StartmobileSms.send(:phone => '30971234567', :text => 'Hello!', :out_number => 'service_name') # Check sms delivery status StartmobileSms.check(sms_id) # Or use instance object sms = StartmobileSms::Message.new(:phone => '30971234567', :text => 'Hello!') sms.phone # 30971234567 sms.text # Hello! sms.send # send sms to server sms.id # response id sms.status # check sms status (Accepted, Enroute, Delivered, Expired, Deleted, Undeliverable, Rejected
4 notes &
I have Facebook contest album with many photos. And I need to display rating of this photos by user likes.
This FQL query get a list of all likes (user_ids) by all photos:
SELECT user_id, object_id FROM like WHERE object_id IN (SELECT object_id FROM photo WHERE aid=’139307129471375_51887’ limit 5000) limit 100000
Then you can order results by object_id and sort by object_id count.
NOTE! Album ID (‘139307129471375_51887’) - is not facebook graph object id. 139307129471375 - is page id, 51887 - element id for this page.
For my example I use Ruby and fb_graph gem:
query = FbGraph::Query.new(“SELECT user_id, object_id FROM like WHERE object_id IN (SELECT object_id FROM photo WHERE aid=’139307129471375_51887’ limit 5000) limit 100000 “).fetch(SiteConfig.fb_token)
SiteConfig.fb_token - is my facebook app access_token with permissions user_photos (get it easy by using Graph API Explorer)
Then I order and sort my array:
photos = query.inject(Hash.new(0)){ |h, e| h[e[“object_id”]] += 1 ; h }.sort{|a,b| b[1]<=>a[1]}
photos.each {|p| puts “Object id - #{p[0]}, likes count - #{p[1]}”}
38 notes &
Редирект странички средствами Rails: