zh_kostev_ext 0.1.16 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,6 +1,89 @@
1
- = zh_kostev_ext
1
+ == ZhKostevExt - some usefull method container
2
+
3
+ == Installation
4
+
5
+ Add it to your Gemfile:
6
+
7
+ gem "zh_kostev_ext"
8
+
9
+ Run the following command to install it:
10
+
11
+ bundle install
12
+
13
+ == Usage
14
+
15
+ <b>ZhKostevExt</b> add some methods. Here is list of this methods:
16
+
17
+ == In ActionController::Base
18
+
19
+ ===== find_resource_or_404 #Load resource or redirect to 404 if can't load resource. Used as before filter.
20
+ You can specify DB_MODEL constant in controller if controller name doesn't connects with model which it used.
21
+ DB_MODEL = :contact
22
+ You can specify DB_SCOPE constant in controller if you need to find model in special scope.
23
+ DB_SCOPE = :sent
24
+ You can specify DB_ID_PARAM constant in controller if you pass to controller not id, but smth else.
25
+ DB_ID_PARAM = :contact_id
26
+ You can specify DB_SCOPE and DB_MODEL and DB_ID_PARAM together
27
+ class ContactsController < ApplicationController
28
+ DB_MODEL = :contact
29
+ DB_SCOPE = :sent
30
+ DB_ID_PARAM = :contact_id
31
+ before_filter :find_resource_or_404, :only => [:edit,:show, :destroy, :update] #it will find model as Contact.sent.find(params[:contact_id])
32
+
33
+ ===== store_location #stores request path if request is get and not ajax and controller is not session_controller
34
+ Example
35
+ after_filter :store_location
36
+
37
+ ===== 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)
38
+ You can specify REDIRECT_DEFAULT_PATH constant in ApplicationController to setup default redirect path(by default REDIRECT_DEFAULT_PATH='/')
39
+ You can also pass params to redirect to method. Example
40
+ redirect_back_or_default :test_param => "test"
41
+
42
+ ===== add_params_to_url #add params to url. Prevent situation with identical param name
43
+ Example:
44
+ add_params_to_url('localhost:3000?test=3', {:test => 5, :asd => 2} #result will be 'localhost:3000?test=5&asd=2'
45
+
46
+ ===== Gem add ability to set default params(override default url_for method).
47
+ Example if you set in HomeController:
48
+ before_filter { @hash_of_additional_params = {:test => '123'} } #this will add test param to all urls in home views
49
+ 'auctions_path' return '/auctions' by default, but after you set example before filter
50
+ 'auctions_path' will return '/auctions?test=123'
51
+
52
+ == In ExportToExcel module
53
+ Nowadays export to excel files is common task. ExportToExcel module provide export_to_excel_prototype method.
54
+ It uses gem speadsheet in order to export to excel.
55
+ First you should include this module in your controller
56
+ include ControllerExtensions::ExportToExcel
57
+ Then use export_to_excel_prototype method. It will create excel file with data and send it to user. See example below:
58
+ class PermissionSetsController < ApplicationController
59
+ # ["First Name", "Last Name"] names of the columns in excel
60
+ # %w(first_name last_name) mthods, which will be called on user to set values in columns
61
+ def export_to_excel
62
+ records = User.where(:super_admin => true).all #find record to export
63
+ export_to_excel_prototype(records, ["First Name", "Last Name"], %w(first_name last_name))
64
+ end
65
+ end
66
+
67
+ You can specify file_name to provide name of excel file
68
+
69
+ export_to_excel_prototype(records, ["First Name", "Last Name"], %w(first_name last_name), :file_name => "Super Admin excel export.xls")
70
+
71
+ You can specify worksheet_name to provide name of worksheet in excel file
72
+
73
+ 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")
74
+
75
+ == In Hash class
76
+
77
+ ===== humanized #This hash return humanized value of key(except moments when key has set another value)
78
+ Example
79
+ a = Hash.humanized
80
+ a[:vladimir] = 'Vlad'
81
+
82
+ a[:misha] will return 'Misha'
83
+ a[:vladimir] will return 'Vlad'(because this value set manually)
84
+
85
+
2
86
 
3
- Description goes here.
4
87
 
5
88
  == Contributing to zh_kostev_ext
6
89
 
@@ -16,4 +99,3 @@ Description goes here.
16
99
 
17
100
  Copyright (c) 2012 zh.kostev. See LICENSE.txt for
18
101
  further details.
19
-
@@ -9,9 +9,11 @@ module ControllerExtensions
9
9
  # Example:
10
10
  #
11
11
  # class PermissionSetsController < ApplicationController
12
+ # include ControllerExtensions::ExportToExcel
13
+ #
12
14
  # def export_to_excel
13
15
  # records = User.where(:super_admin => true).all #find record to export
14
- # export_to_excel_prototype(records, ["First Name", "Last Name"], %w(first_name, last_name))
16
+ # export_to_excel_prototype(records, ["First Name", "Last Name"], %w(first_name last_name))
15
17
  # end
16
18
  # end
17
19
  #
@@ -17,13 +17,17 @@ module ControllerExtensions
17
17
  # You can specify DB_SCOPE constant in controller if you need to find model in special scope.
18
18
  #
19
19
  # DB_SCOPE = :sent
20
+ # You can specify DB_ID_PARAM constant in controller if you pass to controller not id, but smth else.
21
+ #
22
+ # DB_ID_PARAM = :contact_id
20
23
  #
21
24
  # You can specify DB_SCOPE and DB_MODEL together
22
25
  #
23
- # class PermissionSetsController < ApplicationController
26
+ # class ContactsController < ApplicationController
24
27
  # DB_MODEL = :contact
25
28
  # DB_SCOPE = :sent
26
- # before_filter :find_resource_or_404, :only => [:edit,:show, :destroy, :update] #it will find model as Contact.sent.find(params[:id])
29
+ # DB_ID_PARAM = :contact_id
30
+ # before_filter :find_resource_or_404, :only => [:edit,:show, :destroy, :update] #it will find model as Contact.sent.find(params[:contact_id])
27
31
  #
28
32
  # Valid Options:
29
33
  #
@@ -31,11 +35,9 @@ module ControllerExtensions
31
35
  def find_resource_or_404
32
36
  model_name = defined?(self.class::DB_MODEL) ? self.class::DB_MODEL : controller_name.singularize
33
37
  model_to_find = defined?(self.class::DB_SCOPE) ? model_name.to_s.classify.constantize.send(self.class::DB_SCOPE) : model_name.to_s.classify.constantize
34
- begin
35
- instance_variable_set("@#{model_name.to_s.underscore}", model_to_find.find(params[:id]))
36
- rescue
37
- redirect_to '/404'
38
- end
38
+ id_from_param = defined?(self.class::DB_ID_PARAM) ? params[self.class::DB_ID_PARAM.to_sym] : params[:id]
39
+
40
+ instance_variable_set("@#{model_name.to_s.underscore}", model_to_find.find(id_from_param))
39
41
  end
40
42
 
41
43
  #redirects to back.
@@ -66,14 +68,15 @@ module ControllerExtensions
66
68
  # add_params_to_url('localhost:3000?test=3', {:test => 5, :asd => 2} #result will be 'localhost:3000?test=5&asd=2'
67
69
  def add_params_to_url(url, options)
68
70
  main_url, main_params = url.split('?')
69
- if main_params
71
+ options = {} if options.nil?
72
+ if main_params && main_params.split('&')
70
73
  main_params = Hash[main_params.split('&').map { |el| el.split("=") }]
71
74
  main_params = main_params.symbolize_keys
72
75
  options = options.symbolize_keys
73
76
  options = main_params.merge(options)
74
77
  end
75
78
 
76
- main_url + "#{'?' unless options.blank?}"+options.map { |key, value| "#{key}=#{value}" }.join("&")
79
+ main_url + "#{'?' unless options.blank?}" + options.map { |key, value| "#{key}=#{value}" }.join("&")
77
80
  end
78
81
  end
79
82
  end
data/lib/zh_kostev_ext.rb CHANGED
@@ -4,7 +4,6 @@ require "controller_extensions/url_ext"
4
4
 
5
5
  class ActionController::Base
6
6
  include ControllerExtensions::GeneralMethods
7
- include ControllerExtensions::ExportToExcel
8
7
  include ControllerExtensions::UrlExt
9
8
  end
10
9
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zh_kostev_ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.16
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-12 00:00:00.000000000 Z
12
+ date: 2013-01-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: spreadsheet
@@ -123,7 +123,9 @@ dependencies:
123
123
  - - ! '>='
124
124
  - !ruby/object:Gem::Version
125
125
  version: '0'
126
- description: GEM IN DEVELOPMENT MODE
126
+ description: ! "ZhKostevExt add some methods. It add some methods to ActionController::Base
127
+ and Hash class.\n Gem contains export to excel module to make export easier.
128
+ See readme https://github.com/ZhKostev/zh_kostev_ext"
127
129
  email: zh.kostev@gmail.com
128
130
  executables: []
129
131
  extensions: []
@@ -153,7 +155,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
153
155
  version: '0'
154
156
  segments:
155
157
  - 0
156
- hash: -818128301
158
+ hash: 4217705574523471923
157
159
  required_rubygems_version: !ruby/object:Gem::Requirement
158
160
  none: false
159
161
  requirements:
@@ -162,8 +164,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
162
164
  version: '0'
163
165
  requirements: []
164
166
  rubyforge_project:
165
- rubygems_version: 1.8.21
167
+ rubygems_version: 1.8.24
166
168
  signing_key:
167
169
  specification_version: 3
168
- summary: GEM IN DEVELOPMENT MODE
170
+ summary: Some usefull method container
169
171
  test_files: []