Trailblazer cells in the Rails console - cells

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)

Related

Netzke with file uploads, preferably carrierwave

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

Rails add partial to Simpleform custom inputs

I'm creating a custom SimpleForm input. It utilizes a javascript modal popup to select values.
I can basically get it to work if I include the modal partial in the form view, but I would ideally like to have the custom input push the partial to the template.
However, I can't get it to work. I'm trying this, but it doesn't render the partial correctly.
class ProductCategoryImageSelectInput < SimpleForm::Inputs::CollectionSelectInput
def input
html = ''
html << 'Launch demo modal'
File.open("#{Rails.root}/app/views/product_categories/_browse_modal.html.erb", 'r') do |f|
html << f.read
end
end
end
Any thoughts?
EDIT
I'm apparently too new of a user to post screenshots, but here are links to the differences in functionality:
When I embed the modal code in the form, I get this, which is the desired functionality:
working
When I try to put the modal code in the custom input, I get this instead:
not working
You can also render partial from a custom input using the SimpleForm::FormBuilder instance which is assigned to an instance variable named #builder:
class ProductCategoryImageSelectInput < SimpleForm::Inputs::CollectionSelectInput
def input
#builder.template.render(partial: 'product_categories/browse_modal', locals: { foo: 'bar'})
end
end
Weirdly, it seems I've already answered it here.
Does this work for you?
ERB.new(f.read).result(binding)
PS: Updated with ERB instead of HAML.

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

rails-3.0.0rc with jquery as default

In a new rail 3 app I want to replace prototype with jquery, but my initializer script:
module ActionView::Helpers::AssetTagHelper
remove_const :JAVASCRIPT_DEFAULT_SOURCES
JAVASCRIPT_DEFAULT_SOURCES = %w(jquery-1.4.1.min rails)
reset_javascript_include_default
end
won't work any more. I get:
constant ActionView::Helpers::AssetTagHelper::JAVASCRIPT_DEFAULT_SOURCES not defined
Any ideas?
inside your config/application.rb
config.action_view.javascript_expansions[:defaults] = ['jquery-1.4.2', 'rails']