Sha256: 1e138059bc3983bfc8879d684d1ee53e210099c870db30aad1fec3a88caf8b5c

Contents?: true

Size: 1.45 KB

Versions: 6

Compression:

Stored size: 1.45 KB

Contents

#require "active_record"
require 'deep_cloneable'
module Chouette
  class ActiveRecord < ::ActiveRecord::Base

    self.abstract_class = true

    before_save :nil_if_blank

    # to be overrided to set nullable attrs when empty
    def self.nullable_attributes
      []
    end

    def nil_if_blank
      self.class.nullable_attributes.each { |attr| self[attr] = nil if self[attr].blank? }
    end


    def human_attribute_name(*args)
      self.class.human_attribute_name(*args)
    end

    class << self
      alias_method :create_reflection_without_chouette_naming, :create_reflection

      def create_reflection(macro, name, options, active_record)
        options = 
          Reflection.new(macro, name, options, active_record).options_with_default

        create_reflection_without_chouette_naming(macro, name, options, active_record)
      end
    end



    class Reflection

      attr_reader :macro, :name, :options, :active_record

      def initialize(macro, name, options, active_record)
        @macro, @name, @options, @active_record = macro, name.to_s, options, active_record
      end

      def collection?
        macro == :has_many
      end

      def singular_name
        collection? ? name.singularize : name
      end

      def class_name
        "Chouette::#{singular_name.camelize}"
      end

      def options_with_default
        options.dup.tap do |options|
          options[:class_name] ||= class_name
        end
      end

    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ninoxe-1.1.5 app/models/chouette/active_record.rb
ninoxe-1.1.4 app/models/chouette/active_record.rb
ninoxe-1.1.3 app/models/chouette/active_record.rb
ninoxe-1.1.2 app/models/chouette/active_record.rb
ninoxe-1.1.1 app/models/chouette/active_record.rb
ninoxe-1.1.0 app/models/chouette/active_record.rb