Class: Faalis::Dashboard::DSL::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/faalis/dashboard/dsl/base.rb

Direct Known Subclasses

Create, Index, Update

Instance Attribute Summary collapse

Instance Method Summary collapse

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_buttonsObject (readonly)

Returns the value of attribute action_buttons.



5
6
7
# File 'lib/faalis/dashboard/dsl/base.rb', line 5

def action_buttons
  @action_buttons
end

#default_scopeObject (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

#fieldsObject (readonly)

Returns the value of attribute fields.



5
6
7
# File 'lib/faalis/dashboard/dsl/base.rb', line 5

def fields
  @fields
end

#modelObject (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_button(**options)
  @action_buttons ||= []
  @action_buttons << options
  @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, **options, &block)
  if options.include? :except
    @fields = resolve_model_reflections.reject do |field|
      options[:except].include? field.to_sym
    end
  elsif options.include? :append

    @fields += options[: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