Sha256: f9acabb9739084982ccc1bb93de164342a2b1966269b9b94f60a99df02fcc76a

Contents?: true

Size: 1.78 KB

Versions: 11

Compression:

Stored size: 1.78 KB

Contents

=begin
  Camaleon CMS is a content management system
  Copyright (C) 2015 by Owen Peredo Diaz
  Email: owenperedo@gmail.com
  This program is free software: you can redistribute it and/or modify   it under the terms of the GNU Affero General Public License as  published by the Free Software Foundation, either version 3 of the  License, or (at your option) any later version.
  This program is distributed in the hope that it will be useful,  but WITHOUT ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the  GNU Affero General Public License (GPLv3) for more details.
=end
module ActiveRecordExtras
  module Relation
    extend ActiveSupport::Concern

    module ClassMethods
      def update_or_create(attributes = {})
        assign_or_new(attributes).save
      end

      def update_or_create!(attributes = {})
        assign_or_new(attributes).save!
      end

      def assign_or_new(attributes)
        obj = first || new
        obj.assign_attributes(attributes)
        obj
      end
    end
  end
end

ActiveRecord::Base.send :include, ActiveRecordExtras::Relation

ActiveRecord::Associations::CollectionProxy.class_eval do
  # order a collection by custom fields
  # Arguments:
  # key: (String) Custom field key
  # order: (String) order direction (ASC | DESC)
  # sample: Site.first.posts.sort_by_field("untitled-field-attributes", "desc")
  def sort_by_field(key, order = "ASC")
    # class_name = self.build.class.name
    # table_name = class_name.classify.table_name
    self.includes(:custom_field_values).where("#{CustomFieldsRelationship.table_name}.custom_field_slug = ? and #{CustomFieldsRelationship.table_name}.object_class = ?", key, self.build.class.name).reorder("#{CustomFieldsRelationship.table_name}.value #{order}")
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
camaleon_cms-1.1.0 config/initializers/active_record_extension.rb
camaleon_cms-1.0.9 config/initializers/active_record_extension.rb
camaleon_cms-1.0.8 config/initializers/active_record_extension.rb
camaleon_cms-1.0.7 config/initializers/active_record_extension.rb
camaleon_cms-1.0.6 config/initializers/active_record_extension.rb
camaleon_cms-1.0.5 config/initializers/active_record_extension.rb
camaleon_cms-1.0.4 config/initializers/active_record_extension.rb
camaleon_cms-1.0.3 config/initializers/active_record_extension.rb
camaleon_cms-1.0.2 config/initializers/active_record_extension.rb
camaleon_cms-1.0.1 config/initializers/active_record_extension.rb
camaleon_cms-1.0 config/initializers/active_record_extension.rb