Module: CaseyJones::ActiveRecord
- Defined in:
- lib/casey_jones/string_reader.rb,
lib/casey_jones/active_record_extensions.rb
Defined Under Namespace
Classes: StringReader
Instance Method Summary collapse
- #acts_as_sitemap ⇒ Object
- #anaf_habtm(association, options = {}) ⇒ Object
- #association_text(association, opts = {}) ⇒ Object
- #autocomplete_format(&block) ⇒ Object
- #condition(cond) ⇒ Object
- #equals_condition(str) ⇒ Object
- #like_condition(str) ⇒ Object
- #lookup(params) ⇒ Object
-
#make_scope_string(find_opts) ⇒ Object
Needs to call scopes on a class and pass it values that are pulled from a hash called obj_attr.
- #named(str) ⇒ Object
- #named_association(member, attribute, opts = {}) ⇒ Object
- #search_on(*cols) ⇒ Object
Instance Method Details
#acts_as_sitemap ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/casey_jones/active_record_extensions.rb', line 4 def acts_as_sitemap class_eval do has_many :sitemaps, :as=>:resource def self.sitemap. if self.sitemap end def (t) self.sitemap ||= Sitemap.new(:resource=>self) self.sitemap. = t unless t.empty? end end end |
#anaf_habtm(association, options = {}) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/casey_jones/active_record_extensions.rb', line 43 def anaf_habtm(association, ={}) ar_opts = [:ar_options] ||= {} klass = ar_opts[:class_name] ||= association.to_s.singularize.camelize class_eval do has_and_belongs_to_many association.to_sym, ar_opts end find_line = "obj = obj ||= #{klass}.#{make_scope_string(options[:find])}.first" if .has_key?(:find) association = association.to_s.underscore.tableize scope_string = class_eval " def \#{association}_attributes=(attr_hash)\n obj_coll = []\n attr_hash.each_value do |obj_attr|\n next if obj_attr[\"_destroy\"] == \"1\"\n obj = \#{klass}.find(obj_attr[\"id\"]) if obj_attr.has_key?(\"id\")\n obj_attr = obj_attr.reject_keys [\"id\", \"_destroy\"]\n \#{find_line}\n if obj && obj.update_attributes(obj_attr)\n obj_coll << obj\n elsif (obj = \#{klass}.new(obj_attributes)) && obj.save\n obj_coll << obj\n end\n end\n self.\#{association} = obj_coll\n end\n" end |
#association_text(association, opts = {}) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/casey_jones/active_record_extensions.rb', line 71 def association_text(association, opts={}) raise "Must give a name for text_association" unless opts.has_key?(:name) class_eval do if opts.has_key?(:class_name) klass = opts[:class_name].constantize else klass = association.to_s.singularize.camelize.constantize end define_method "#{association.to_s}_text=", lambda{ |text| return if text.empty? coll = StringReader.new.read_items(text) do |name, commentary| a = klass.undecorate(name) if klass.methods.include?("undecorate") if opts.has_key?(:scope) obj = self.send(association).scopes[opts[:scope]].call(name).first end params = {opts[:name]=>name} params[opts[:commentary]] = commentary if opts.has_key?(:commentary) obj = obj ||= klass.new obj = klass.find(obj) unless obj.new_record? obj.decoration = a if a obj.attributes = params obj.save obj end self.send("#{association.to_s}=", coll) } define_method "#{association.to_s}_text", lambda{ StringReader.new.write_items(self.send(association.to_s)) do |item| name = item.send(opts[:name]) name = item.decorate(name) if item.methods.include?("decorate") [name, opts.has_key?(:commentary) ? item.send(opts[:commentary]) : ""] end } end end |
#autocomplete_format(&block) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/casey_jones/active_record_extensions.rb', line 20 def autocomplete_format(&block) class_eval do @autocomplete_block = block def self.to_autocomplete(items) items.map{|t| @autocomplete_block.call(t)}.to_json end end end |
#condition(cond) ⇒ Object
172 173 174 |
# File 'lib/casey_jones/active_record_extensions.rb', line 172 def condition(cond) search_columns.map{|c| "trim(lower(#{c})) #{cond}"}.join(" or ") end |
#equals_condition(str) ⇒ Object
163 164 165 |
# File 'lib/casey_jones/active_record_extensions.rb', line 163 def equals_condition(str) where(condition("= '#{str.downcase}'")) end |
#like_condition(str) ⇒ Object
159 160 161 |
# File 'lib/casey_jones/active_record_extensions.rb', line 159 def like_condition(str) where(condition("ilike '%#{str}%'")) end |
#lookup(params) ⇒ Object
149 150 151 152 153 154 155 156 157 |
# File 'lib/casey_jones/active_record_extensions.rb', line 149 def lookup(params) str = params.kind_of?(Hash) ? params[:id] : params str = params[:id] if str.match(/\D/) named(str) else find(str) end end |
#make_scope_string(find_opts) ⇒ Object
Needs to call scopes on a class and pass it values that are
pulled from a hash called obj_attr
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/casey_jones/active_record_extensions.rb', line 31 def make_scope_string(find_opts) _scopes = [] find_opts.each{ |scope, attr_name| if attr_name.class == Hash _scopes << "#{scope.to_s}(obj_attr[#{name.first[0].to_s}][#{name.first[1].to_s}])" else _scopes << "#{scope.to_s}(obj_attr[#{name.to_s}])" end } _scopes.join(".") end |
#named(str) ⇒ Object
168 169 170 |
# File 'lib/casey_jones/active_record_extensions.rb', line 168 def named(str) with_name(str).first end |
#named_association(member, attribute, opts = {}) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/casey_jones/active_record_extensions.rb', line 108 def named_association(member, attribute, opts={}) member = member.to_s klass = (opts.has_key?(:class_name) ? opts[:class_name] : member.to_s.singularize.camelize).constantize attribute = attribute.to_s if opts.has_key?(:create) class_eval do define_method "#{member}_#{attribute}=", lambda{|value| return if value.blank? obj = klass.named(value) obj = obj ||= klass.create(attribute => value) self.send("#{member}=", obj) } end else class_eval do define_method "#{member}_#{attribute}=", lambda{|value| self.send("#{member}=",klass.named(value)) unless value.blank? } end end class_eval "def #{member}_#{attribute}; #{member}.#{attribute} if #{member}; end;" end |
#search_on(*cols) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/casey_jones/active_record_extensions.rb', line 133 def search_on(*cols) class_eval "def self.search_columns; #{cols.map{|t| t.to_s}.to_ary.inspect}; end;" class_eval do scope :search, lambda{|str| items = like_condition(str.downcase) if scopes.has_key?(:search_mod) items = items.search_mod end items } scope :with_name, lambda{|str| equals_condition(str.downcase).limit(1) } end end |