diff options
| author | erdgeist <erdgeist@erdgeist.org> | 2026-06-25 04:34:55 +0200 |
|---|---|---|
| committer | erdgeist <erdgeist@erdgeist.org> | 2026-06-25 04:34:55 +0200 |
| commit | f7a5944a1f44ede9881d368a36eb9f7d82d6ab69 (patch) | |
| tree | 3803e9840eab4976b299a8cce969b7785b018b19 /app/models | |
| parent | 3f236c7a0e544db94ef822f120d649ac5ee958f7 (diff) | |
Rails 4.2 model and controller fixes
- Wrap all scopes in lambdas (required in Rails 4)
- Move scopes after associations in page.rb (evaluated at load time in Rails 4)
- Convert association :order options to lambda syntax
- Remove attr_accessible from page.rb and user.rb
- Add Strong Parameters: user_params in UsersController, node_params/page_params in NodesController
- Fix clone_attributes_from: exclude id/page_id/timestamps when cloning translations
- Fix redirect_to :back → request.referer fallback in nodes_controller
- Fix node_path/publish and unlock actions: pass @node argument
Diffstat (limited to 'app/models')
| -rw-r--r-- | app/models/asset.rb | 9 | ||||
| -rw-r--r-- | app/models/menu_item.rb | 4 | ||||
| -rw-r--r-- | app/models/node.rb | 2 | ||||
| -rw-r--r-- | app/models/page.rb | 15 | ||||
| -rw-r--r-- | app/models/permission.rb | 4 | ||||
| -rw-r--r-- | app/models/related_asset.rb | 6 | ||||
| -rw-r--r-- | app/models/user.rb | 2 |
7 files changed, 19 insertions, 23 deletions
diff --git a/app/models/asset.rb b/app/models/asset.rb index 3ad5857..f6526f2 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb | |||
| @@ -13,8 +13,9 @@ class Asset < ActiveRecord::Base | |||
| 13 | :headline => "460x250#" | 13 | :headline => "460x250#" |
| 14 | } | 14 | } |
| 15 | ) | 15 | ) |
| 16 | 16 | ||
| 17 | scope :images, where(:upload_content_type => ["image/gif", "image/jpeg", "image/png"]) | 17 | scope :images, -> { where(:upload_content_type => ["image/gif", "image/jpeg", "image/png"]) } |
| 18 | scope :documents, where(:upload_content_type => ["application/pdf", "text/plain", "text/rtf"]) | 18 | scope :documents, -> { where(:upload_content_type => ["application/pdf", "text/plain", "text/rtf"]) } |
| 19 | scope :audio, where(:upload_content_type => ["audio/mpeg", "audio/x-m4a", "audio/wav", "audio/x-wav"]) | 19 | scope :audio, -> { where(:upload_content_type => ["audio/mpeg", "audio/x-m4a", "audio/wav", "audio/x-wav"]) } |
| 20 | |||
| 20 | end | 21 | end |
diff --git a/app/models/menu_item.rb b/app/models/menu_item.rb index d1ddc68..eb82347 100644 --- a/app/models/menu_item.rb +++ b/app/models/menu_item.rb | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | class MenuItem < ActiveRecord::Base | 1 | class MenuItem < ActiveRecord::Base |
| 2 | 2 | ||
| 3 | default_scope where(:type => "MenuItem") | 3 | default_scope -> { where(:type => "MenuItem") } |
| 4 | 4 | ||
| 5 | translates :title | 5 | translates :title |
| 6 | 6 | ||
| @@ -24,5 +24,5 @@ end | |||
| 24 | 24 | ||
| 25 | 25 | ||
| 26 | class FeaturedArticle < MenuItem | 26 | class FeaturedArticle < MenuItem |
| 27 | default_scope where(:type => "FeaturedArticle") | 27 | default_scope -> { where(:type => "FeaturedArticle") } |
| 28 | end | 28 | end |
diff --git a/app/models/node.rb b/app/models/node.rb index 3cab7ed..8be6daf 100644 --- a/app/models/node.rb +++ b/app/models/node.rb | |||
| @@ -3,7 +3,7 @@ class Node < ActiveRecord::Base | |||
| 3 | acts_as_nested_set | 3 | acts_as_nested_set |
| 4 | 4 | ||
| 5 | # Associations | 5 | # Associations |
| 6 | has_many :pages, :order => "revision ASC", :dependent => :destroy | 6 | has_many :pages, -> { order("revision ASC") }, :dependent => :destroy |
| 7 | belongs_to :head, :class_name => "Page", :foreign_key => :head_id, :dependent => :destroy | 7 | belongs_to :head, :class_name => "Page", :foreign_key => :head_id, :dependent => :destroy |
| 8 | belongs_to :draft, :class_name => "Page", :foreign_key => :draft_id, :dependent => :destroy | 8 | belongs_to :draft, :class_name => "Page", :foreign_key => :draft_id, :dependent => :destroy |
| 9 | has_many :permissions, :dependent => :destroy | 9 | has_many :permissions, :dependent => :destroy |
diff --git a/app/models/page.rb b/app/models/page.rb index 5c93a93..e2cbee5 100644 --- a/app/models/page.rb +++ b/app/models/page.rb | |||
| @@ -5,10 +5,6 @@ class Page < ActiveRecord::Base | |||
| 5 | PUBLIC_TEMPLATE_PATH = File.join(%w(custom page_templates public)) | 5 | PUBLIC_TEMPLATE_PATH = File.join(%w(custom page_templates public)) |
| 6 | FULL_PUBLIC_TEMPLATE_PATH = Rails.root.join('app', 'views', PUBLIC_TEMPLATE_PATH) | 6 | FULL_PUBLIC_TEMPLATE_PATH = Rails.root.join('app', 'views', PUBLIC_TEMPLATE_PATH) |
| 7 | 7 | ||
| 8 | # named scopes | ||
| 9 | scope :drafts, joins(:node).includes(:translations).where("nodes.draft_id = pages.id") | ||
| 10 | scope :heads, joins(:node).includes(:translations).where("nodes.head_id = pages.id") | ||
| 11 | |||
| 12 | # Mixins and Plugins | 8 | # Mixins and Plugins |
| 13 | acts_as_taggable | 9 | acts_as_taggable |
| 14 | acts_as_list :column => :revision, :scope => :node_id | 10 | acts_as_list :column => :revision, :scope => :node_id |
| @@ -20,16 +16,17 @@ class Page < ActiveRecord::Base | |||
| 20 | belongs_to :user | 16 | belongs_to :user |
| 21 | belongs_to :editor, :class_name => "User" | 17 | belongs_to :editor, :class_name => "User" |
| 22 | has_many :related_assets | 18 | has_many :related_assets |
| 23 | has_many :assets, :through => :related_assets, :order => "position ASC" | 19 | has_many :assets, -> { order("position ASC") }, :through => :related_assets |
| 20 | |||
| 21 | # Named scopes | ||
| 22 | scope :drafts, -> { joins(:node).includes(:translations).where("nodes.draft_id = pages.id") } | ||
| 23 | scope :heads, -> { joins(:node).includes(:translations).where("nodes.head_id = pages.id") } | ||
| 24 | 24 | ||
| 25 | # Filter | 25 | # Filter |
| 26 | before_create :set_page_title | 26 | before_create :set_page_title |
| 27 | before_create :set_template | 27 | before_create :set_template |
| 28 | before_save :rewrite_links_in_body | 28 | before_save :rewrite_links_in_body |
| 29 | 29 | ||
| 30 | # Security | ||
| 31 | attr_accessible :title, :abstract, :body, :template_name, :published_at, :user_id | ||
| 32 | |||
| 33 | # Class Methods | 30 | # Class Methods |
| 34 | 31 | ||
| 35 | # This method is most likely called from the ContentHelper.render_collection | 32 | # This method is most likely called from the ContentHelper.render_collection |
| @@ -146,7 +143,7 @@ class Page < ActiveRecord::Base | |||
| 146 | 143 | ||
| 147 | # Clone translated attributes | 144 | # Clone translated attributes |
| 148 | page.translations.each do |translation| | 145 | page.translations.each do |translation| |
| 149 | self.translations.create!(translation.attributes) | 146 | self.translations.create!(translation.attributes.except("id", "page_id", "created_at", "updated_at")) |
| 150 | end | 147 | end |
| 151 | 148 | ||
| 152 | # Clone asset references | 149 | # Clone asset references |
diff --git a/app/models/permission.rb b/app/models/permission.rb index a7a30ed..f304538 100644 --- a/app/models/permission.rb +++ b/app/models/permission.rb | |||
| @@ -8,6 +8,6 @@ class Permission < ActiveRecord::Base | |||
| 8 | belongs_to :node | 8 | belongs_to :node |
| 9 | 9 | ||
| 10 | # Named scopes | 10 | # Named scopes |
| 11 | scope :for_node, lambda { |node| where('node_id = ?', (node.is_a?(Node) ? node.id : node)) } | 11 | scope :for_node, ->(node) { where('node_id = ?', (node.is_a?(Node) ? node.id : node)) } |
| 12 | scope :for_user, lambda { |user| where('user_id = ?', (user.is_a?(User) ? user.id : user)) } | 12 | scope :for_user, ->(user) { where('user_id = ?', (user.is_a?(User) ? user.id : user)) } |
| 13 | end | 13 | end |
diff --git a/app/models/related_asset.rb b/app/models/related_asset.rb index af09420..2b61c51 100644 --- a/app/models/related_asset.rb +++ b/app/models/related_asset.rb | |||
| @@ -3,6 +3,6 @@ class RelatedAsset < ActiveRecord::Base | |||
| 3 | belongs_to :asset | 3 | belongs_to :asset |
| 4 | 4 | ||
| 5 | acts_as_list :scope => :page_id | 5 | acts_as_list :scope => :page_id |
| 6 | 6 | ||
| 7 | default_scope :order => "position ASC" | 7 | default_scope -> { order("position ASC") } |
| 8 | end \ No newline at end of file | 8 | end |
diff --git a/app/models/user.rb b/app/models/user.rb index ce5503f..a2540b5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb | |||
| @@ -21,8 +21,6 @@ class User < ActiveRecord::Base | |||
| 21 | validates_format_of :email, :with => Authentication.email_regex, | 21 | validates_format_of :email, :with => Authentication.email_regex, |
| 22 | :message => Authentication.bad_email_message | 22 | :message => Authentication.bad_email_message |
| 23 | 23 | ||
| 24 | attr_accessible :login, :email, :password, :password_confirmation, :admin | ||
| 25 | |||
| 26 | # Authenticates a user by their login name and unencrypted password. Returns the user or nil. | 24 | # Authenticates a user by their login name and unencrypted password. Returns the user or nil. |
| 27 | def self.authenticate(login, password) | 25 | def self.authenticate(login, password) |
| 28 | return nil if login.blank? || password.blank? | 26 | return nil if login.blank? || password.blank? |
