Monday, December 29, 2008

Customizing ActiveScaffold

Customizing Active Scaffold according to your requirements is very easy.

Example :- Removing create, edit or delete links from your page and add your own links

Also add nested links if this model is related to other model. Example there is another model name as Product which is related to Category model say category has many products and product belongs to Category.

For that we need to do the following

Now in controller we need to write the following code:

active_scaffold :category do |config|

config.label = "Category" # Display page name

config.columns = [:name, :rank, :description, :products]

config.columns[:name].label = "Some Name" # customize column name

update.columns.exclude :rank # exclude rank column during edit

list.columns.exclude :description #exclude description column in list

#this is a extra link which is applicable for every row if type record and if type is table then it is for all records

config.action_links.add "cat",:label=>"Categories",:type=>:record

#this is a link to products where you can list add edit delete and associate products to category

config.nested.add_link "Add Products", [:products]

end

def cat

#your required code

end

Considering product model we have fields like product_name, product_description, category_id

active_scaffold :product do |config|

config.label = "Product Page" # Display page name

# Category_id not needed by default it will take while adding

config.columns = [:product_name, :product_description]

config.columns[:product_name].label = "Product Name"

list.columns.exclude :product_description

end

and we need to add the required code in Product_Helper.rb as we did in category_helper.rb

and also we can add our own code if required during update and create by copying the code from …\vendor\plugins\active_scaffold\lib\actions to your controller and edit it as required

Now in your page you can add and associate products to category.

Search in ActiveScaffold

Integrating Search in Active Scaffold is very easy

In controller we need to write the following code to make it work:

Considering the category we have

active_scaffold :category do |config|

config.label = "Category" # Display page name

config.columns = [:name, :rank, :description, :products]

config.columns[:name].label = "Some Name"

update.columns.exclude :rank # exclude rank column during edit

# Search the list by name

config.columns[:name].search_sql = "name"

config.search.columns << :name

# Search the list by product name

config.columns[:products].search_sql = "products.name"

config.search.columns << :products

end

We can search both the parent and child i.e Category and Product names in the same page.

File Column Plugin in ActiveScaffold

Integrating File Column Plugin in Active Scaffold is very easy

Assuming File Column Plugin is installed in your application.

In controller we need to write the following code to make it work:

Considering there is a field called Product_Image in product

active_scaffold :product do |config|

config.label = "Product Page"

config.columns = [:product_name, :product_description, :product_image]

config.create.multipart = true

config.update.multipart = true

config.columns[:product_image].label = "Product Image"

end

Its not yet done we need to do few coding in helper and model also

In Product_Helper.rb we need to add the following code

def product_image_form_column(record, input_name)

file_column_field 'record', :product_image, :onkeypress=>"return numbersonly(event, false)"

end

#onkeypress event is used to advoid typing characters in file field

def product_image_column(record)

image_tag url_for_file_column(record, "product_image") if record.product_image

end

and In Product_Image.rb we need to write a single line code

file_column :product_image

Its Done and when you upload a image it will create that image in a folder named by the Product_id in public/Product_images and will insert the image name in product_image product table

FCK Editor Plugin in ActiveScaffold

Integrating FCK Editor Plugin in Active Scaffold is very easy

Assuming FCK Editor Plugin is installed in your application.

In controller we need to write the following code to make it work:

Considering the category we have this as usual

active_scaffold :category do |config|

config.label = "Category" # Display page name

config.columns = [:name, :rank, :description] # all fields are taken

list.columns.exclude :description #exclude description column in list

end

In Category_Helper.rb we need to add the following code

#description field is shown in FCK editor format

def description_form_column(record, input_name)

fckeditor_textarea( :record, :description, :toolbarSet => 'Simple', :name=> input_name, :width => "800px", :height => "200px" )

end

#description text is displayed in rich format

def description_column(record)

sanitize(record.description)

end

and we need to copy the “_create_form.rhtml” & “_update_form.rhtml” from ..\vendor\plugins\active_scaffold\frontends\default\views to Views/Category and edit _create_form.rhtml by adding following code to it

Instead of <%= submit_tag as_('Create'), :class => "submit" %>

we add following code

="submit" value="Create" class="submit"

onClick="var oEditor = FCKeditorAPI.GetInstance('record_<%=@record.id%>_<%='description'%>_editor');

document.getElementById('record_<%=@record.id%>_<%='description'%>_editor').value = oEditor.GetXHTML();" />

and the same in _update_form.rhtml just replacing the value “Create” to “Update”

and your code will be working fine.