The amount of code that increases and the readability that decreases when you write it as you want without thinking about it ... In order to summarize as concisely as possible, we have summarized the methods that can be used for each MVC for beginners.
MVC | role |
---|---|
Model | Manipulation of data handled by the application/processing |
View | Displaying information to the user, receiving input from the user |
Controller | Exchange information with Model and View |
Understand this role and describe the processing appropriate for each role. If you write in each file as you like without considering the role, maintenance will become difficult. For example, if the process to be described in Model is described in Controller, Fat Controller Problems such as (Controller with a lot of code written and swollen) are likely to occur.
Considering each role of MVC mentioned above, the method responsible for data processing / processing is described in Model.
app/controllers/posts_controller.rb
class PostsController < ApplicationController
def index
@posts = Post.order(created_at: :desc).limit(10)
end
end
Defined as a class method so that it can be called directly to the Model class.
app/models/post.rb
class Post < ApplicationRecord
def self.latest(number)
order(created_at: :desc).limit(number)
end
end
Call a method on the Controller
app/controllers/posts_controller.rb
class PostsController < ApplicationController
def index
@posts = Post.latest(10)
end
end
You can also use scope instead of class methods.
app/models/post.rb
class Post < ApplicationRecord
scope :latest, -> (number = 10){order(created_at: :desc).limit(number)}
end
app/controllers/posts_controller.rb
class PostsController < ApplicationController
def index
@posts = Post.latest
end
end
When the same process is repeatedly described in multiple Models, Concern can be used to cut out the common process.
app/models/post.rb
scope :latest, -> (number = 10){order(created_at: :desc).limit(number)}
def hoge
puts "hogehoge"
end
app/models/comment.rb
scope :latest, -> (number = 10){order(created_at: :desc).limit(number)}
def hoge
puts "hogehoge"
end
Create a file in app / models / concerts and describe common processing.
app/models/concerns/common_module.rb
module CommonModule
extend ActiveSupport::Concern
included do
scope :latest, -> (number = 10){order(created_at: :desc).limit(number)}
def self.hoge
puts "hogehoge"
end
end
end
Common processing can be used by including in each model.
app/models/post.rb
include CommonModule
app/models/comment.rb
include CommonModule
The description of validation, which tends to be long, can be cut out to the validator. Create a validators directory under the app directory and create a file for validation.
app/validators/post_validator.rb
class PostValidator < ActiveModel::Validator
def validate(record)
prohibited_words = ["baka","aho"]
if prohibited_words.any?{ |word| record.content.include?(word) }
record.errors.add(:content, "Contains prohibited words.")
end
end
end
Describe additionally on the Model side.
app/models/post.rb
validates_with PostValidator
Callbacks can also be cut out from the Model. Create an app / callbacks directory and create a file for callbacks. (The class name should be the same as the callback name)
app/callbacks/post_callback.rb
class PostCallback
def before_create(post)
post.title = "No Title" if post.title.blank?
end
end
Describe additionally on the Model side.
app/models/post.rb
before_create PostCallback.new
Recommended Posts