activesupport-concern

How to create a Rails 4 Concern that takes an argument

梦想的初衷 提交于 2020-01-28 06:30:13
问题 I have an ActiveRecord class called User. I'm trying to create a concern called Restrictable which takes in some arguments like this: class User < ActiveRecord::Base include Restrictable # Would be nice to not need this line restrictable except: [:id, :name, :email] end I want to then provide an instance method called restricted_data which can perform some operation on those arguments and return some data. Example: user = User.find(1) user.restricted_data # Returns all columns except :id,

Overriding methods in an ActiveSupport::Concern module which are defined by a class method in the same module

守給你的承諾、 提交于 2019-12-09 02:51:44
问题 I have an ActiveSupport::Concern module which looks roughly like the following: module MyModel module Acceptance extend ActiveSupport::Concern included do enum status: [:declined, :accepted] end def declined! self.status = :declined # some extra logic self.save! end def accepted! self.status = :accepted # some extra logic self.save! end end end This is only ever going to be included into ActiveRecord classes, hence the use of enum . Basically, I'm overriding the declined! and accepted!

what is difference between using concerns vs modules in rails?

爱⌒轻易说出口 提交于 2019-12-08 15:48:27
问题 Just now I started to using Concerns in rails, but i have doubt why we go for concerns, because we can achieve same thing on module & mixing concept. So please any one tell about shat is the use of concerns instead of using module. 回答1: It's well described here: http://api.rubyonrails.org/classes/ActiveSupport/Concern.html In short: Concerns allow you to use #included and #class_methods instead of self.included hook with additional module ClassMethods creation; Concerns give you a better

How to define a state_machine in a Concern?

99封情书 提交于 2019-12-06 04:50:42
问题 I am trying to factor out some duplicated logic into a concern. Part of the duplicated logic is a state_machine. Simplified, the Database , Site , SftpUser and more contain, amongst others, this: class Database < ActiveRecord::Base # ... state_machine :deploy_state, initial: :halted do state :pending end end I'm attempting to refactor this into a concern: module Deployable extend ActiveSupport::Concern included do state_machine :deploy_state, initial: :halted do state :pending end end end #

How to define a state_machine in a Concern?

老子叫甜甜 提交于 2019-12-04 10:40:08
I am trying to factor out some duplicated logic into a concern . Part of the duplicated logic is a state_machine . Simplified, the Database , Site , SftpUser and more contain, amongst others, this: class Database < ActiveRecord::Base # ... state_machine :deploy_state, initial: :halted do state :pending end end I'm attempting to refactor this into a concern: module Deployable extend ActiveSupport::Concern included do state_machine :deploy_state, initial: :halted do state :pending end end end # Tested with: class DeployableDouble < ActiveRecord::Base extend Deployable end describe

How to test a Controller Concern in Rails 4

房东的猫 提交于 2019-12-03 00:33:26
问题 What is the best way to handle testing of concerns when used in Rails 4 controllers? Say I have a trivial concern Citations . module Citations extend ActiveSupport::Concern def citations ; end end The expected behavior under test is that any controller which includes this concern would get this citations endpoint. class ConversationController < ActionController::Base include Citations end Simple. ConversationController.new.respond_to? :yelling #=> true But what is the right way to test this

How to test a Controller Concern in Rails 4

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 14:08:16
What is the best way to handle testing of concerns when used in Rails 4 controllers? Say I have a trivial concern Citations . module Citations extend ActiveSupport::Concern def citations ; end end The expected behavior under test is that any controller which includes this concern would get this citations endpoint. class ConversationController < ActionController::Base include Citations end Simple. ConversationController.new.respond_to? :yelling #=> true But what is the right way to test this concern in isolation? class CitationConcernController < ActionController::Base include Citations end

Overriding methods in an ActiveSupport::Concern module which are defined by a class method in the same module

佐手、 提交于 2019-12-01 03:34:46
I have an ActiveSupport::Concern module which looks roughly like the following: module MyModel module Acceptance extend ActiveSupport::Concern included do enum status: [:declined, :accepted] end def declined! self.status = :declined # some extra logic self.save! end def accepted! self.status = :accepted # some extra logic self.save! end end end This is only ever going to be included into ActiveRecord classes, hence the use of enum . Basically, I'm overriding the declined! and accepted! methods that are created by ActiveRecord::Enum.enum with some extra, custom logic of my own. The problem is,