Class: RailsOmnibar

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_omnibar.rb,
lib/rails_omnibar/items.rb,
lib/rails_omnibar/config.rb,
lib/rails_omnibar/engine.rb,
lib/rails_omnibar/version.rb,
lib/rails_omnibar/commands.rb,
lib/rails_omnibar/item/base.rb,
lib/rails_omnibar/item/help.rb,
lib/rails_omnibar/rendering.rb,
lib/rails_omnibar/command/base.rb,
lib/rails_omnibar/command/search.rb

Defined Under Namespace

Modules: Command, Item Classes: BaseController, Engine, JsController, QueriesController

Constant Summary collapse

VERSION =
'1.3.2'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#calculator=(value) ⇒ Object (writeonly)

Sets the attribute calculator

Parameters:

  • value

    the value to set the attribute calculator to.



15
16
17
# File 'lib/rails_omnibar/config.rb', line 15

def calculator=(value)
  @calculator = value
end

#modal=(value) ⇒ Object (writeonly)

Sets the attribute modal

Parameters:

  • value

    the value to set the attribute modal to.



10
11
12
# File 'lib/rails_omnibar/config.rb', line 10

def modal=(value)
  @modal = value
end

#placeholderObject



29
30
31
32
33
34
# File 'lib/rails_omnibar/config.rb', line 29

def placeholder
  return @placeholder.presence unless @placeholder.nil?

  help_item = items.find { |i| i.type == :help }
  help_item && "Hint: Type `#{help_item.title}` for help"
end

Class Method Details

.cast_to_command(arg) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/rails_omnibar/commands.rb', line 16

def self.cast_to_command(arg)
  case arg
  when Command::Base then arg
  when Hash          then Command::Base.new(**arg)
  else raise(ArgumentError, "expected command, got #{arg.class}")
  end
end

.cast_to_item(arg) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/rails_omnibar/items.rb', line 13

def self.cast_to_item(arg)
  case arg
  when Item::Base then arg
  when Hash       then Item::Base.new(**arg)
  else raise(ArgumentError, "expected Item, got #{arg.class}")
  end
end

.configure(&block) ⇒ Object



6
7
8
# File 'lib/rails_omnibar.rb', line 6

def self.configure(&block)
  @singleton = block_given? ? new.tap(&block) : new
end

.likeObject



65
66
67
# File 'lib/rails_omnibar/command/search.rb', line 65

def self.like
  @like ||= ActiveRecord::Base.connection.adapter_name =~ /^post|pg/i ? 'ILIKE' : 'LIKE'
end

Instance Method Details

#add_command(command) ⇒ Object



10
11
12
13
14
# File 'lib/rails_omnibar/commands.rb', line 10

def add_command(command)
  commands << RailsOmnibar.cast_to_command(command)
  clear_cache
  self
end

#add_help(**kwargs) ⇒ Object



2
3
4
5
# File 'lib/rails_omnibar/item/help.rb', line 2

def add_help(**kwargs)
  add_item Item::Help.new(for_commands: commands, **kwargs)
  self.class
end

#add_item(item) ⇒ Object



2
3
4
5
6
# File 'lib/rails_omnibar/items.rb', line 2

def add_item(item)
  items << RailsOmnibar.cast_to_item(item)
  clear_cache
  self.class
end

#add_items(*args) ⇒ Object



8
9
10
11
# File 'lib/rails_omnibar/items.rb', line 8

def add_items(*args)
  args.each { |arg| add_item(arg) }
  self.class
end

#add_record_search(**kwargs) ⇒ Object



6
7
8
# File 'lib/rails_omnibar/command/search.rb', line 6

def add_record_search(**kwargs)
  add_command Command::RecordSearch.new(**kwargs)
end

#add_search(**kwargs) ⇒ Object



2
3
4
# File 'lib/rails_omnibar/command/search.rb', line 2

def add_search(**kwargs)
  add_command Command::Search.new(**kwargs)
end

#as_jsonObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rails_omnibar/rendering.rb', line 13

def as_json(*)
  {
    calculator:     calculator?,
    commandPattern: JsRegex.new!(command_pattern, target: 'ES2018'),
    hotkey:         hotkey,
    items:          items,
    maxResults:     max_results,
    modal:          modal?,
    placeholder:    placeholder,
    queryPath:      urls.query_path(omnibar_class: self.class),
  }
end

#calculator?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/rails_omnibar/config.rb', line 16

def calculator?
  instance_variable_defined?(:@calculator) ? !!@calculator : true
end

#command_patternObject



6
7
8
# File 'lib/rails_omnibar/commands.rb', line 6

def command_pattern
  commands.any? ? Regexp.union(commands.map(&:pattern)) : /$NO_COMMANDS/
end

#handle(input) ⇒ Object



2
3
4
# File 'lib/rails_omnibar/commands.rb', line 2

def handle(input)
  commands.find { |h| h.pattern.match?(input) }&.then { |h| h.call(input, self) } || []
end

#hotkeyObject



20
21
22
# File 'lib/rails_omnibar/config.rb', line 20

def hotkey
  @hotkey || 'k'
end

#hotkey=(arg) ⇒ Object



23
24
25
26
# File 'lib/rails_omnibar/config.rb', line 23

def hotkey=(arg)
  arg.to_s.size == 1 || raise(ArgumentError, 'hotkey must have length 1')
  @hotkey = arg.to_s.downcase
end

#max_resultsObject



6
7
8
# File 'lib/rails_omnibar/config.rb', line 6

def max_results
  @max_results || 10
end

#max_results=(arg) ⇒ Object



2
3
4
5
# File 'lib/rails_omnibar/config.rb', line 2

def max_results=(arg)
  arg.is_a?(Integer) && arg > 0 || raise(ArgumentError, 'max_results must be > 0')
  @max_results = arg
end

#modal?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/rails_omnibar/config.rb', line 11

def modal?
  instance_variable_defined?(:@modal) ? !!@modal : false
end

#renderObject



2
3
4
5
6
7
8
9
# File 'lib/rails_omnibar/rendering.rb', line 2

def render
  @cached_html ||= <<~HTML.html_safe
    <script src='#{urls.js_path}?v=#{RailsOmnibar::VERSION}' type='text/javascript'></script>
    <div id='mount-rails-omnibar'>
      <script type="application/json">#{to_json}</script>
    </div>
  HTML
end

#urlsObject



26
27
28
# File 'lib/rails_omnibar/rendering.rb', line 26

def urls
  @urls ||= RailsOmnibar::Engine.routes.url_helpers
end