Sha256: d8199a1d0dacb779d478b042b0d2d4d55f6d72957aa131197ff8731b9cfdee78

Contents?: true

Size: 1.03 KB

Versions: 18

Compression:

Stored size: 1.03 KB

Contents

module RestAssured
  module Models
    class Redirect < ActiveRecord::Base
      attr_accessible :pattern, :to, :position

      validates_presence_of :pattern, :to

      scope :ordered, order('position')

      before_create :assign_position

      def self.find_redirect_url_for(fullpath)
        if redirect = ordered.find { |r| fullpath =~ /#{r.pattern}/ }
          fullpath.sub /#{redirect.pattern}/, redirect.to
        end
      end

      def self.update_order(ordered_redirect_ids)
        success = true

        transaction do
          begin
            ordered_redirect_ids.each_with_index do |r_id, idx|
              r = find(r_id)
              r.position = idx
              r.save!
            end
          rescue => e
            # TODO log exception
            puts e.inspect
            success = false
            raise ActiveRecord::Rollback
          end
        end
        success
      end

      private

      def assign_position
        self.position = ( self.class.maximum(:position) || -1 ) + 1
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
rest-assured-2.0.0 lib/rest-assured/models/redirect.rb
rest-assured-1.2.2 lib/rest-assured/models/redirect.rb
rest-assured-1.2.1 lib/rest-assured/models/redirect.rb
rest-assured-1.2.0 lib/rest-assured/models/redirect.rb
rest-assured-1.1.10 lib/rest-assured/models/redirect.rb
rest-assured-1.1.9 lib/rest-assured/models/redirect.rb
rest-assured-1.1.8 lib/rest-assured/models/redirect.rb
rest-assured-1.1.7 lib/rest-assured/models/redirect.rb
rest-assured-1.1.6 lib/rest-assured/models/redirect.rb
rest-assured-1.1.5 lib/rest-assured/models/redirect.rb
rest-assured-1.1.4 lib/rest-assured/models/redirect.rb
rest-assured-1.1.3 lib/rest-assured/models/redirect.rb
rest-assured-1.1.2 lib/rest-assured/models/redirect.rb
rest-assured-1.1.1 lib/rest-assured/models/redirect.rb
rest-assured-1.1.0 lib/rest-assured/models/redirect.rb
rest-assured-1.0.0 lib/rest-assured/models/redirect.rb
rest-assured-1.0.0.rc2 lib/rest-assured/models/redirect.rb
rest-assured-1.0.0.rc1 lib/rest-assured/models/redirect.rb