Skip to content

Commit

Permalink
Merge pull request #1441 from tvdeyen/belongs_to-optional
Browse files Browse the repository at this point in the history
Use optional: true for optional belongs_to associations
  • Loading branch information
tvdeyen authored May 14, 2018
2 parents ddefb26 + b0e1bb4 commit d86a655
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/models/alchemy/cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Alchemy
class Cell < BaseRecord
include Alchemy::Logger

belongs_to :page, required: true
belongs_to :page, inverse_of: :cells
validates_uniqueness_of :name, scope: 'page_id'
validates_format_of :name, with: /\A[a-z0-9_-]+\z/
has_many :elements, -> { where(parent_element_id: nil).order(:position) }, dependent: :destroy
Expand Down
4 changes: 2 additions & 2 deletions app/models/alchemy/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class Content < BaseRecord
# Concerns
include Alchemy::Content::Factory

belongs_to :essence, required: true, polymorphic: true, dependent: :destroy
belongs_to :element, required: true, touch: true
belongs_to :essence, polymorphic: true, dependent: :destroy
belongs_to :element, touch: true, inverse_of: :contents
has_one :page, through: :element

stampable stamper_class_name: Alchemy.user_class_name
Expand Down
6 changes: 3 additions & 3 deletions app/models/alchemy/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ class Element < BaseRecord
foreign_key: :parent_element_id,
dependent: :destroy

belongs_to :cell, required: false, touch: true
belongs_to :page, required: true, touch: true
belongs_to :cell, optional: true, touch: true
belongs_to :page, touch: true, inverse_of: :descendent_elements

# A nested element belongs to a parent element.
belongs_to :parent_element,
class_name: 'Alchemy::Element',
required: false,
optional: true,
touch: true

has_and_belongs_to_many :touchable_pages, -> { distinct },
Expand Down
2 changes: 1 addition & 1 deletion app/models/alchemy/essence_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

module Alchemy
class EssenceFile < BaseRecord
belongs_to :attachment, required: false
belongs_to :attachment, optional: true
acts_as_essence ingredient_column: 'attachment'

def attachment_url
Expand Down
2 changes: 1 addition & 1 deletion app/models/alchemy/essence_picture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Alchemy
class EssencePicture < BaseRecord
acts_as_essence ingredient_column: 'picture'

belongs_to :picture, required: false
belongs_to :picture, optional: true
delegate :image_file_width, :image_file_height, :image_file, to: :picture
before_save :fix_crop_values
before_save :replace_newlines
Expand Down
5 changes: 2 additions & 3 deletions app/models/alchemy/folded_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@

module Alchemy
class FoldedPage < BaseRecord
belongs_to :page, required: true
belongs_to :user, required: true,
class_name: Alchemy.user_class_name
belongs_to :page, inverse_of: :folded_pages
belongs_to :user, inverse_of: :folded_pages, class_name: Alchemy.user_class_name

def self.folded_for_user(user)
return none unless Alchemy.user_class < ActiveRecord::Base
Expand Down
2 changes: 1 addition & 1 deletion app/models/alchemy/language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

module Alchemy
class Language < BaseRecord
belongs_to :site, required: true
belongs_to :site
has_many :pages

before_validation :set_locale, if: -> { locale.blank? }
Expand Down
2 changes: 1 addition & 1 deletion app/models/alchemy/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Page < BaseRecord

stampable stamper_class_name: Alchemy.user_class_name

belongs_to :language, required: false
belongs_to :language, optional: true

has_one :site, through: :language
has_many :site_languages, through: :site, source: :languages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Alchemy
context 'with `remarkable_type` being an allowed type' do
it 'is successful' do
get :index, params: {remarkable_type: 'elements'}
expect(response).to be_success
expect(response).to be_successful
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/alchemy/pages_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ module Alchemy

it "renders page" do
get :show, params: {urlname: still_public_page.urlname}
expect(response).to be_success
expect(response).to be_successful
end
end

Expand All @@ -207,7 +207,7 @@ module Alchemy

it "renders page" do
get :show, params: {urlname: still_public_page.urlname}
expect(response).to be_success
expect(response).to be_successful
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/dummy/app/models/dummy_user.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

class DummyUser < ActiveRecord::Base
has_many :folded_pages, class_name: 'Alchemy::FoldedPage'
attr_writer :alchemy_roles, :name

def self.logged_in
Expand Down

0 comments on commit d86a655

Please sign in to comment.