Last active
August 29, 2015 13:57
-
-
Save Jrizzi1/9665655 to your computer and use it in GitHub Desktop.
unique validation across models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Slugable | |
extend ActiveSupport::Concern | |
included do | |
validates :slug, uniqueness: true, presence: true, exclusion: { in: %w[admin] } | |
before_validation :generate_slug | |
end | |
def link | |
"/#{self.slug}" | |
end | |
private | |
def generate_slug | |
return true if self.slug.present? | |
self.slug = self.name.to_s.gsub(/[^[:alnum:]]+/, '-').downcase | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Widget < ActiveRecord::Base | |
include Slugable | |
validates :name, presence: true | |
validate :uniqueness_of_a_slug_across_models, if: 'has_sprocket?' | |
def uniqueness_of_a_slug_across_models | |
errors.add(:slug, "can't be shared slug") | |
end | |
def has_sprocket! | |
@has_sprocket = true | |
end | |
def has_sprocket? | |
!!@has_sprocket | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Admin::WidgetsController < ApplicationController | |
before_action :set_widget, only: [:show, :edit, :update, :destroy] | |
# GET /widgets | |
# GET /widgets.json | |
def index | |
@widgets = Widget.all | |
end | |
# GET /widgets/1 | |
# GET /widgets/1.json | |
def show | |
end | |
# GET /widgets/new | |
def new | |
@widget = Widget.new | |
end | |
# GET /widgets/1/edit | |
def edit | |
end | |
# POST /widgets | |
# POST /widgets.json | |
def create | |
@widget = Widget.new(widget_params) | |
@widget.has_sprocket! if Sprocket.find_by(slug: params[:widget_slug]) | |
respond_to do |format| | |
if @widget.save | |
format.html { redirect_to [:admin, @widget], notice: 'widget was successfully created.' } | |
format.json { render action: 'show', status: :created, location: @widget } | |
else | |
format.html { render action: 'new' } | |
format.json { render json: @widget.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# PATCH/PUT /widgets/1 | |
# PATCH/PUT /widgets/1.json | |
def update | |
respond_to do |format| | |
if @widget.update(widget_params) | |
format.html { redirect_to [:admin, @widget], notice: 'widget was successfully updated.' } | |
format.json { head :no_content } | |
else | |
format.html { render action: 'edit' } | |
format.json { render json: @widget.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# DELETE /widgets/1 | |
# DELETE /widgets/1.json | |
def destroy | |
@widget.destroy | |
respond_to do |format| | |
format.html { redirect_to admin_widgets_url } | |
format.json { head :no_content } | |
end | |
end | |
private | |
# Use callbacks to share common setup or constraints between actions. | |
def set_widget | |
@widget = Widget.find(params[:id]) | |
end | |
# Never trust parameters from the scary internet, only allow the white list through. | |
def widget_params | |
params.require(:widget).permit(:name, :slug, :description) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment