Class: Faalis::Dashboard::DSL::Base
- Inherits:
-
Object
- Object
- Faalis::Dashboard::DSL::Base
- Defined in:
- lib/faalis/dashboard/dsl/base.rb
Instance Attribute Summary collapse
-
#action_buttons ⇒ Object
readonly
Returns the value of attribute action_buttons.
-
#default_scope ⇒ Object
readonly
Returns the value of attribute default_scope.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
-
#action_button(**options) ⇒ Object
Define a new action on the ‘action` place of the current section options: Is a hash which contains the action button properties.
-
#attributes(*fields_name, **options, &block) ⇒ Object
Allow user to specify an array of model attributes to be used in respected section.
-
#initialize(model) ⇒ Base
constructor
Base class for all the DSL property classes to be used as the yielded object inside each section DSL scope.
-
#scope(name = nil, &block) ⇒ Object
Return the default scope for current properties object.
Constructor Details
#initialize(model) ⇒ Base
Base class for all the DSL property classes to be used as the yielded object inside each section DSL scope.
For example the below code will yield an instance of one of this class children.
in_index do
# This will yield an instance of this class child
end
The most important note in this class is that all the public methods that their name starts with an underscore (_) not meant to be used as DSL.
22 23 24 25 26 27 |
# File 'lib/faalis/dashboard/dsl/base.rb', line 22 def initialize(model) @action_buttons = [] @model = model @fields = resolve_model_reflections @fields_type = {} end |
Instance Attribute Details
#action_buttons ⇒ Object (readonly)
Returns the value of attribute action_buttons.
5 6 7 |
# File 'lib/faalis/dashboard/dsl/base.rb', line 5 def @action_buttons end |
#default_scope ⇒ Object (readonly)
Returns the value of attribute default_scope.
5 6 7 |
# File 'lib/faalis/dashboard/dsl/base.rb', line 5 def default_scope @default_scope end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
5 6 7 |
# File 'lib/faalis/dashboard/dsl/base.rb', line 5 def fields @fields end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
5 6 7 |
# File 'lib/faalis/dashboard/dsl/base.rb', line 5 def model @model end |
Instance Method Details
#action_button(**options) ⇒ Object
Define a new action on the ‘action` place of the current section options: Is a hash which contains the action button properties.
‘name`: Label to use with the button. `href`: The link href to use in the `a` tag `class`: classes of the button. `icon_class`: font awesome icon to use in button.
93 94 95 96 97 |
# File 'lib/faalis/dashboard/dsl/base.rb', line 93 def (**) @action_buttons ||= [] @action_buttons << @action_buttons.uniq! end |
#attributes(*fields_name, **options, &block) ⇒ Object
Allow user to specify an array of model attributes to be used in respected section. For example attributes to show as header columns in index section
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/faalis/dashboard/dsl/base.rb', line 32 def attributes(*fields_name, **, &block) if .include? :except @fields = resolve_model_reflections.reject do |field| [:except].include? field.to_sym end elsif .include? :append @fields += [:append] else # Check for valid field names fields_name.each do |name| unless @fields.include? name.to_s raise ArgumentError, "can't find '#{name}' field for model '#{model}'." end end # set new value for fields @fields = fields_name.map(&:to_s) unless fields_name.empty? end @fields.concat(block.call.map(&:to_s)) if block_given? end |
#scope(name = nil, &block) ⇒ Object
Return the default scope for current properties object. scope will be used to fetch all the records for the given section. The Default implementation belongs to index section and return a page aware list of all the objects.
You can easily change it via the corresponding section dsl. For example in case of ‘index` section:
in_index do |index|
index.scope do |params|
YourModel.all
end
# or ...
scope :my_scope
end
private
def my_scope
YourModel.where(...)
end
Arguments:
-
params: Is the same params passed to controller action.
81 82 83 84 |
# File 'lib/faalis/dashboard/dsl/base.rb', line 81 def scope(name = nil, &block) return @default_scope = block if block_given? @default_scope = name.to_sym end |