(Not documented)
# File vendor/plugins/ubiquo_core/lib/ubiquo/extensions/active_record.rb, line 6 6: def self.extended(klass) 7: # create a paginate alias for ubiquo_paginate unless already exists 8: alias_method :paginate, :ubiquo_paginate unless klass.respond_to?('paginate') 9: end
Function for apply an array of scopes. see self.create_scopes documentation for an example
# File vendor/plugins/ubiquo_core/lib/ubiquo/extensions/active_record.rb, line 97
97: def apply_find_scopes(scopes, &initial)
98: scopes.compact.inject(initial) do |block, value|
99: Proc.new do
100: with_scope({:find => value}, &block)
101: end
102: end.call
103: end
iterate over filters and stores each returned value in an array. returns an array with all returned values of each iteration. example of usage:
scopes = create_scopes(filters) do |filter, value|
case filter
when :string
{:conditions => ["upper(tasks.name) LIKE upper(?) or upper(tasks.description) LIKE upper(?)", "%#{value}%", "%#{value}%"]}
when :time
case value.to_s
when "current"
{:conditions => ["tasks.status < 100"]}
when "old"
{:conditions => ["tasks.status = 100"]}
end
when :project
{:conditions => ["tasks.project_id = ?", value.to_i]}
when :type
{:conditions => ["tasks.task_type_id = ?", value.to_i]}
when :ubiquo_user
{:conditions => ["tasks.owner_id = ?", value.to_i]}
when :current_ubiquo_user
scope_options_for_ubiquo_user(value)
end
end apply_find_scopes(scopes) do
find(:all, :include => [:task_type, :owner, {:project => [:owner, :project_permissions]}])
end
# File vendor/plugins/ubiquo_core/lib/ubiquo/extensions/active_record.rb, line 133
133: def create_scopes(filters)
134: filters.inject([]) do |acc, (key, value)|
135: next acc if value.nil? || (value.respond_to?(:empty?) ? value.empty? : false)
136: scope = yield(key, value)
137: acc + [scope]
138: end
139: end
Intermediate method customizing paperclip has_attached_file Calls paperclip with id_partition (folders style 000/000/001) in path and url params
# File vendor/plugins/ubiquo_core/lib/ubiquo/extensions/active_record.rb, line 46
46: def file_attachment(field, options = {})
47: options.reverse_merge!(Ubiquo::Config.get(:attachments))
48: visibility = options[:visibility]
49:
50: # Comment it because we didn't achieved run path with lambda
51: # v = nil
52: # path = lambda { |obj|
53: # v = visibility.is_a?(Proc) ? visibility.call(obj.instance) : visibility
54: # v_path = options["#{v}_path".to_sym]
55: # ":rails_root/#{v_path}/media/:attachment/:id_partition/:style/:basename.:extension"
56: # }
57: Paperclip::Interpolations[:visibility_prefix] = lambda do |attachment, style|
58: '/ubiquo/attachment' if attachment.instance.respond_to?(:is_protected) && attachment.instance.is_protected
59: end
60: path = ":rails_root/#{visibility}/media/:class/:attachment/:id_partition/:style/:filename"
61: define_method("#{field}_is_public?") do
62: visibility.to_sym == :public
63: end
64: styles = Marshal.load(Marshal.dump(options[:styles])) || {}
65: processors = options[:processors] || [:thumbnail]
66: s3 = {:key => '', :secret =>'', :bucket => ''}
67: if File.exists?("#{Rails.root}/config/s3.yml")
68: s3_config = YAML.load_file("#{Rails.root}/config/s3.yml")
69: s3[:key] = s3_config[Rails.env]['access_key_id']
70: s3[:secret] = s3_config[Rails.env]['secret_access_key']
71: s3[:bucket] = s3_config[Rails.env]['bucket']
72: end
73:
74: if options[:storage] == :s3
75: url = ':s3_domain_url'
76: path = "media/:class/:attachment/:id_partition/:style/:filename"
77: else
78: url = ":visibility_prefix/media/:class/:attachment/:id_partition/:style/:filename"
79: end
80:
81: has_attached_file field,
82: :url => url,
83: :path => path,
84: :styles => styles,
85: :processors => processors,
86: :whiny => false,
87: :storage => options[:storage] || :filesystem,
88: :s3_credentials => {
89: :access_key_id => s3[:key],
90: :secret_access_key => s3[:secret],
91: :bucket => s3[:bucket]
92: }
93: end
TODO: Make use of alias and add test for it
# File vendor/plugins/ubiquo_core/lib/ubiquo/extensions/active_record.rb, line 12
12: def han(args)
13: human_attribute_name(args)
14: end
Applies a limit and offset scope that allows to easily paginate model results options can be the following:
:page => current page (default 1) :per_page => number of elements per page (default: :elements_per_page in Ubiquo Config)
Returns an array composed of [{
:previous => nil or the previous page number :next => nil or the next page number }, requested items
]
# File vendor/plugins/ubiquo_core/lib/ubiquo/extensions/active_record.rb, line 28
28: def ubiquo_paginate(options = {})
29: options.delete_if{|o1,o2| o2.blank? }
30: options.reverse_merge!({:page => 1, :per_page => Ubiquo::Config.get(:elements_per_page) })
31: items = self.with_scope(:find => {:limit => (options[:per_page].to_i + 1), :offset => (options[:per_page].to_i * (options[:page].to_i - 1))}) do
32: yield
33: end
34: [
35: {
36: :previous => (options[:page].to_i > 1 ? options[:page].to_i - 1 : nil),
37: :next => (items.delete_at(options[:per_page].to_i).nil? ? nil : options[:page].to_i + 1)
38: },
39: items
40: ]
41: end
Disabled; run with $DEBUG to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.