class ArDocStore::Attributes::EmbedsMany

Private Instance Methods

create_attributes_method() click to toggle source
# File lib/ar_doc_store/attributes/embeds_many.rb, line 67
      def create_attributes_method
        model.class_eval <<-CODE, __FILE__, __LINE__ + 1
        def #{attribute}_attributes=(values)
          return if values.blank?

          if values.respond_to?(:each)
            if values.respond_to?(:values)
              values = values.values
            end
          else
            values = [values]
          end
          self.#{attribute} = AssignEmbedsManyAttributes.new(self, #{@class_name}, :#{attribute}, #{attribute}, values).models
        end
        CODE
      end
create_build_method() click to toggle source
# File lib/ar_doc_store/attributes/embeds_many.rb, line 40
      def create_build_method
        model.class_eval <<-CODE, __FILE__, __LINE__ + 1
        def build_#{attribute.to_s.singularize}(attributes = {})
          items = #{attribute} || ArDocStore::EmbeddedCollection.new
          items.embedded_as = :#{attribute}
          items.parent = self
          item = #{@class_name}.new attributes
          item.parent = #{attribute}
          items << item
          self.#{attribute} = items
          item
        end
        CODE
      end
create_ensure_method() click to toggle source
# File lib/ar_doc_store/attributes/embeds_many.rb, line 55
      def create_ensure_method
        attr_singular = attribute.to_s.singularize
        model.class_eval <<-CODE, __FILE__, __LINE__ + 1
        def ensure_#{attribute}(attributes = nil)
          #{attribute}.first || build_#{attr_singular}(attributes)
        end
        def ensure_#{attr_singular}(attributes = nil)
          #{attribute}.first || build_#{attr_singular}(attributes)
        end
        CODE
      end
create_validation() click to toggle source
# File lib/ar_doc_store/attributes/embeds_many.rb, line 84
      def create_validation
        model.class_eval <<-CODE, __FILE__, __LINE__ + 1
          def validate_embedded_record_for_#{attribute}
            validate_embeds_many :#{attribute}
          end
          validate :validate_embedded_record_for_#{attribute}
        CODE
      end
store_attribute() click to toggle source
# File lib/ar_doc_store/attributes/embeds_many.rb, line 8
      def store_attribute
        model.class_eval <<-CODE, __FILE__, __LINE__ + 1
        attribute :#{attribute}, ArDocStore::Types::EmbedsMany.new("#{@class_name}")
        def #{attribute}
          value = read_attribute :#{attribute}
          if value && !value.is_a?(ArDocStore::EmbeddedCollection)
            value = ArDocStore::EmbeddedCollection.new value
          else
            value ||= ArDocStore::EmbeddedCollection.new
          end
          value.parent = self
          value.embedded_as = :#{attribute}
          value.each do |item|
            item.parent = value
          end
          value
        end
        def #{attribute}=(value)
          value = nil if value == '' || value == ['']
          write_attribute :#{attribute}, value
          new_value = read_attribute :#{attribute}
          new_value.parent = self
          new_value.embedded_as = :#{attribute}
          new_value.each do |item|
            item.parent = new_value
          end
          write_store_attribute json_column, :#{attribute}, new_value
          new_value
        end
        CODE
      end