Netzke with file uploads, preferably carrierwave - ruby-on-rails-3

I have a model with a single field with carrierwave for file upload. I searched the google group but don't find a straightforward way to add file uploads to netzke component. I will be happy with doing it in a panel view with a regular browser file upload, i don't need anything fancy.
I just saw one of the demos has a file upload, the Paging form with custom layout. I think I still need guidance how to put it into the Basepack Grid

File upload field should first be added into Basepack::Form (which then can be tested independently):
class AttachmentForm < Netzke::Basepack::Form
def configure(c)
super
c.model = "Attachment"
c.items = [{xtype: :fileuploadfield, name: 'attachment'}]
end
end
Then your grid should be configured to use this form instead of the built-in one:
class AttachmentGrid < Netzke::Basepack::Grid
def configure(c)
super
c.model = 'Attachment'
c.force_fit = true
c.columns = [:link]
c.bbar = [:add_in_form, :del]
end
def preconfigure_record_window(c)
super
c.form_config.klass = AttachmentForm
c.width = 600
c.height = 150
end
end
For reference, here's also the Attachment model:
class Attachment < ActiveRecord::Base
mount_uploader :attachment
# this is a virtual column referred to from `AttachmentGrid`;
# can be moved over to grid itself using `getter` in case
# you object putting HTML into the model
def link
"<a href='#{attachment.url}' target='_blank'>#{attachment.file.try(:identifier)}</a>"
end
end

Related

Trailblazer cells in the Rails console

How can I work with a Trailblazer cell in the Rails console?
The concept helper doesn't work
irb(main):002:0> c = concept(Resource::Cell, l)
NoMethodError: undefined method `concept' for main:Object
Did you mean? concern
context
from (irb):2
concept() & cell() are Controller helpers, meaning they wouldn't work in console if the controller is not loaded. But you don't need them !
The easiest way to play with cell is just to invoke them like . you would with any other ruby object.
Cell
# concepts/song/cell/cell.rb
module Song::Cell
class Index < Trailblazer::Cell
def show
render
end
end
class Show < Trailblazer::Cell
def show
render
end
end
end
View
%h1
Song#show
%p
Find me in app/concepts/song/view/show.haml
Console
# Any of the following calls - they are all equivalent pretty much
# The spit html output of the cell
Song::Cell::Show.(Song.last).()
Song::Cell::Show.(Song.last).render
Song::Cell::Show.(Song.last).(:show)

Retrieving Medium or Large Profile Image from Twitter with omniauth-twitter

I'm using omniauth-twitter gem to authenticate users through twitter. I am also using their Twitter profile image as their avatar for my site. However, the image I get from Twitter is low resolution. I know Twitter has better resolution pics available. How do I get it?
Here is what I am currently doing. It is a method in the user model. It works, just doesn't get me a good quality pic:
user.rb
def update_picture(omniauth)
self.picture = omniauth['info']['image']
end
I thought maybe I could pass a size option onto it somehow, but can not seem to find a good solution.
I'm using the omniauth-twitter gem as well. In the apply_omniauth method of my User model, I save the Twitter image path like this, stripping the _normal suffix:
if omniauth['provider'] == 'twitter'
self.image = omniauth['info']['image'].sub("_normal", "")
end
Then I have a helper method called portrait that accepts a size argument. As Terence Eden suggests, you can just replace the _size suffix of the filename to access the different image sizes that Twitter provides:
def portrait(size)
# Twitter
# mini (24x24)
# normal (48x48)
# bigger (73x73)
# original (variable width x variable height)
if self.image.include? "twimg"
# determine filetype
case
when self.image.downcase.include?(".jpeg")
filetype = ".jpeg"
when self.image.downcase.include?(".jpg")
filetype = ".jpg"
when self.image.downcase.include?(".gif")
filetype = ".gif"
when self.image.downcase.include?(".png")
filetype = ".png"
else
raise "Unable to read filetype of Twitter image for User ##{self.id}"
end
# return requested size
if size == "original"
return self.image
else
return self.image.gsub(filetype, "_#{size}#{filetype}")
end
end
end
Once you have the URL of the image, it's quite simple. You need to remove the "_normal" from the end of the URL.
Here's my avatar image
https://si0.twimg.com/profile_images/2318692719/7182974111_ec8e1fb46f_s_normal.jpg
Here's the larger version
https://si0.twimg.com/profile_images/2318692719/7182974111_ec8e1fb46f_s.jpg
A simple regex should suffice.
Remember, the size of the image is unpredictable - so you may wish to resize it before displaying it on your site.
A better way to do this is through the config options of the omniauth-twitter gem.
provider :twitter, "API_KEY", "API_SECRET", :image_size => 'original'
https://github.com/arunagw/omniauth-twitter

Passing a parameter to the uploader / accessing a model's attribute from within the uploader / letting the user pick the thumbnail size

I would like to crop an image to the size the user has selected from a list (e.g. 100x100px, 200x200px,...)
How would I pass that attribute to the uploader or get the model's attribute from within the uploader?
Accessing the model's attribute from within the uploader as following does not work:
version :thumb do
thumbnail_size = model.thumbnail_size
...
...
end
I get following error:
undefined local variable or method `model' for #
Thank you!
Florian
In order to be able to access the model's attribute I had to add a manipulation helper.
class MyUploader < CarrierWave::Uploader::Base
...
version :thumb do
process :custom_thumbnail
process :convert => 'jpg'
...
end
def custom_thumbnail
width = model.get_image_width
height = model.get_image_height
manipulate! do |img|
img.convert "#{width}x#{height}"
img
end
end
end

Rails 3 Active Admin add preset value to new record

I have tried to do it from the controller and from the active admin override controller and I can't make it work.
A user creates a website.
current_user has an id attribute
website has an user_id attribute
So when I create a new website I want to add the current_user.id into website.user_id. I can't.
Anybody know how?
Right now I need it on the new/create actions but I'll probably need this on the edit/update actions too.
This seems to work for me:
ActiveAdmin.register Website do
controller do
# Do some custom stuff on GET /admin/websites/*/edit
def edit
super do |format|
# Do what you want here ...
#website.user = current_user
end
end
end
end
You should be able to override other controller actions in the same way.
ActiveAdmin.register Model do
# also look in to before_create if hidden on form
before_build do |record|
record.user = current_user
end
end
See https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/resource_dsl.rb#L156
You need to add a 'new' method to the controller. The 'new' method creates an empty website object that will be passed to the form. The default 'new' method just creates an empty #website object. Your 'new' method should create the empty object, and then initialize the value of user to current user:
ActiveAdmin.register Website do
controller do
# Custom new method
def new
#website = Website.new
#website.user = current_user
#set any other values you might want to initialize
end
end

rails 3 custom mime type - default view format

I need to render some views without layout.
To skip line :render :layout=>false and if else logic from controller actions,
i have custom mime type like phtml (plain html).
Mime::Type.register "text/phtml", :phtml
this format need to render the same html views, but only without layout. I complete this with this chunk of code in app. controller:
before_filter proc { |controller|
if params[:format] && params[:format]=='phtml'
controller.action_has_layout = false
controller.request.format = 'html'
end
}
First, this is ugly, second i can't any more control this format from controller in the way:
respond_to :phtml,:only=>:index
because it will always render view with requested format phtml.
is there a better solution? example? how can i alias view formats?
Thank a lot
You can use the layout method in your controller directly:
class ProductsController < ApplicationController
layout "product", :except => [:index, :rss]
end
Or to use no layout at all:
class ProductsController < ApplicationController
layout nil
end
Check out the guide for more info.
I haven't found better solution,only update to my previous example:
before_filter proc { |controller|
if params[:format] && params[:format]=='plain_html' && controller.collect_mimes_from_class_level.include?(:plain_html)
controller.action_has_layout = false
controller.request.format = 'html'
end
}
i added this line to check is a new format defined into our controller:
controller.collect_mimes_from_class_level.include?(:plain_html)
Now we can have full new format which will render our standard html vews rather the build new views for the new format.
This can be useful if we wont to share existing html code, but build different logic based on requested format.
For example, we can easily prepare our html content for print like:
class PagesController < ActionController::Base
layout 'print',:only=>:show
respond_to :plain_html,:only=>[:show]
def show
Page.find(1)
respond_with #page
end
end
And request will be like:
http://www.example.com/pages/1.plain_html
I hope that someone will find this useful.
If u have a better approach for doing this, please share with us.
Regards