ZhKostevExt - some usefull method container
Installation
Add it to your Gemfile:
gem "zh_kostev_ext"
Run the following command to install it:
bundle install
Usage
ZhKostevExt add some methods. Here is list of this methods:
In ActionController::Base
find_resource_or_404 #Load resource or redirect to 404 if can’t load resource. Used as before filter.
You can specify DB_MODEL constant in controller if controller name doesn’t connects with model which it used.
DB_MODEL = :contact
You can specify DB_SCOPE constant in controller if you need to find model in special scope.
DB_SCOPE = :sent
You can specify DB_ID_PARAM constant in controller if you pass to controller not id, but smth else.
DB_ID_PARAM = :contact_id
You can specify DB_SCOPE and DB_MODEL and DB_ID_PARAM together
class ContactsController < ApplicationController
DB_MODEL = :contact
DB_SCOPE = :sent
DB_ID_PARAM = :contact_id
before_filter :find_resource_or_404, :only => [:edit,:show, :destroy, :update] #it will find model as Contact.sent.find(params[:contact_id])
store_location #stores request path if request is get and not ajax and controller is not session_controller
Example
after_filter :store_location
redirect_back_or_default #redirects to back. !IMPORTANT to use this method you should add after_filter to your application_controller(after_filter :store_location)
You can specify REDIRECT_DEFAULT_PATH constant in ApplicationController to setup default redirect path(by default REDIRECT_DEFAULT_PATH=‘/’) You can also pass params to redirect to method. Example
redirect_back_or_default :test_param => "test"
add_params_to_url #add params to url. Prevent situation with identical param name
Example:
add_params_to_url('localhost:3000?test=3', {:test => 5, :asd => 2} #result will be 'localhost:3000?test=5&asd=2'
Gem add ability to set default params(override default url_for method).
Example if you set in HomeController:
before_filter { @hash_of_additional_params = {:test => '123'} } #this will add test param to all urls in home views
‘auctions_path’ return ‘/auctions’ by default, but after you set example before filter
‘auctions_path’ will return ‘/auctions?test=123’
In ExportToExcel module
Nowadays export to excel files is common task. ExportToExcel module provide export_to_excel_prototype method. It uses gem speadsheet in order to export to excel. First you should include this module in your controller
include ControllerExtensions::ExportToExcel
Then use export_to_excel_prototype method. It will create excel file with data and send it to user. See example below:
class PermissionSetsController < ApplicationController
# ["First Name", "Last Name"] names of the columns in excel
# %w(first_name last_name) mthods, which will be called on user to set values in columns
def export_to_excel
records = User.where(:super_admin => true).all #find record to export
export_to_excel_prototype(records, ["First Name", "Last Name"], %w(first_name last_name))
end
end
You can specify file_name to provide name of excel file
export_to_excel_prototype(records, ["First Name", "Last Name"], %w(first_name last_name), :file_name => "Super Admin excel export.xls")
You can specify worksheet_name to provide name of worksheet in excel file
export_to_excel_prototype(records, ["First Name", "Last Name"], %w(first_name last_name), :file_name => "Super Admin excel export.xls", :worksheet_name => "super admins")
In Hash class
humanized #This hash return humanized value of key(except moments when key has set another value)
Example
a = Hash.humanized
a[:vladimir] = 'Vlad'
a[:misha] will return 'Misha'
a[:vladimir] will return 'Vlad'(because this value set manually)
Contributing to zh_kostev_ext
-
Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet.
-
Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it.
-
Fork the project.
-
Start a feature/bugfix branch.
-
Commit and push until you are happy with your contribution.
-
Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Copyright
Copyright © 2012 zh.kostev. See LICENSE.txt for further details.