Select2 With Simple Form in Rails

Select2 is a great JQuery plugin that customizes your select boxes to give a better user interface and experience.

Recently while working on a Rails project, I was experiencing some problems when using Select2 and Simple Form in my view.

In the view, I want to use Select2 to search for people in a Person table in the application's database. Select2 should use AJAX to hit a controller's action that performs the search. The user can then select one of the results found and it will be added to the field as a token (this is a multiple select field).

The controller action looks something like this:

def search
  @people = Person.all.where('name LIKE ? OR last_name LIKE ?',
    "%#{params[:q]}%", "%#{params[:q]}%")

  respond_to do |format|
    format.json { render json: @people.map { |p| { id: p.id, full_name: p.full_name } } }
  end
end

Read more…

An Introduction to Game States in Love2D

After going through countless blog and video tutorials on getting started with Love2D, I found it sad that most people would not take the approach of implementing game states right from the start, or would try to mimick this functionality in a "dirty" way.

After lots of time of reading many forum posts and library documentations. I found a way to implement game states in my games that I am very satisfied with.

In this post I will go over how to implement game states and some of the most common game states seen in games, in such a way that your code is remains organized and neat.

Game States With Stateful.lua

We will be using a simple but great Lua library called Stateful, made by kikito. This library will allow us to easily implement and work with game states inside our game.

This library also depends on the Middleclass library (also by kikito). Middleclass will allow us to construct and develop our game in a OOP (Object Oriented Programing) fashion. If you are familiar with OOP then you will love this approach.


Read more…

Getting Started With Love2D Basics

I recently started playing around with Love2D for game development. Although I am completely new to Lua, so far it's been pretty fun and I really like it.

I created this post to write down the most important basic concepts that I have encountered so far, so that I can use as quick reference later.

Setting Up a Basic IDE

So far I've been using Love2D in Windows. Initially I started using Sublime Text 2 as my editor, but then figured out how to make my Love2D development experience much better using Atom combined with Github for Windows.

In Atom there is a great package called love-ide which has many great tools and features for developming with Love.

Love Atom IDE

Some of these features include Lua linting, autocomplete, and being able to run the Love application right from Atom.

Additionally, we will want to use Window's console for debugging. To do so, enable these options:

Love in Windows

Working With Keyboard Keys

We can interact with user input through the keyboard by determining if a certain key has been pressed. This is done like this:

if love.keyboard.isDown("right") then
  -- ...
end

The love.keyboard.isDown function receives a KeyConstant string argument to indicate which key we want to test for. The complete list can be found here. Here are some examples:

Key string Description
space Space key
backspace Backspace key
down Down arrow key

Read more…

Extending Device Python Classes From Other ZenPacks

Recently I was struggling with trying to make two custom ZenPacks work together with device objects in the Zenoss database. My issue was that a main ZenPack (which would be installed first) would create a custom class that inherits from Device. This ZenPack would add a lot of properties and relationships with custom components. Then, a second optional ZenPack could be installed which would extend this same class with an additional property that would work along with a Python data source.

The zenpack.yaml file for the first and main ZenPack would look something like this:

# ZenPack1

classes:
  SpecialServer:
    base: [zenpacklib.Device]
    label: Special Server

  SpecialComponent:
    base: [zenpacklib.Component]
    label: Special Component
    properties:
      # ...

  AmazingComponent:
    base: [zenpacklib.Component]
    label: Amazing Component
    properties:
      # ...

class_relationships:
  - SpecialServer 1:MC SpecialComponent
  - SpecialServer 1:MC AmazingComponent

Read more…

Using Lambdas in Case Statements in Ruby

In a previous post I talked about how we could use Ruby ranges inside case statements. This was a very neat way of using case statements that would make our code more readable and less repetitive.

The other day I was trying to solve the Bob Ruby exercise in Exercism, and I came up with another very cool way to use case statements: with Lambdas.

The Problem

The Bob problem is very simple. The program receives some input, and it returns some output based on the contents of the input. A strong indication for using a case statement. From the problem's README file:

Bob is a lackadaisical teenager. In conversation, his responses are very limited.

Bob answers 'Sure.' if you ask him a question.

He answers 'Whoa, chill out!' if you yell at him.

He says 'Fine. Be that way!' if you address him without actually saying anything.

He answers 'Whatever.' to anything else.

Solutions

The test provided by Exercism indicates that we should create a Bob class with a hey class method that receives a remark. We could then use a case statement with this remark and evaluate if the remark is a question, yelling, or whatever else needs to be determined according to the instructions. We could create some class methods to determine each of these possibilities:


Read more…