Custom field type with inline_forms

Today I will make an simple field type in inline_forms.

I will use an existing app.

first you need to install the inline_forms gem by typing this in your shell

> gem install inline_forms

your inline_forms gem is now installed.

now let's make an simple app skeleton by typing in your shell :

> inline_forms create lawaway -d sqlite

the shell will output something like this :
Done! Now make your tables with 'bundle exec rails g inline_forms

Go in to the directory via shell

> cd laway

via that directory go to your inline_forms directory by typing in the same shell :

cd `bundle show inline_forms`  

I use atom so I use someting like this :

atom `bundle show inline_forms`  

open your editor by using the shell.

Mac :

open .  

if you don't have mac or don't have an editor that you can launch via the shell, do something like this :

type this in your shell pwd and open that path in your editor.

go to lib/app/helpers/formelements
and duplicate text
field.rb
give it the name textfieldstrange.rb

open textfieldstrange.rb and change this :

InlineForms::SPECIAL_COLUMN_TYPES[:text_field]=:string  

To this :

InlineForms::SPECIAL_COLUMN_TYPES[:text_field_strange]=:string  

why?

Because formelement will be accesible by textfield_strange

also we need to change the three methods name show, edit and update :

def text_field_strange_show(object, attribute)  
def text_field_strange_edit(object, attribute)  
def text_field_strange_update  

We are making a strange text_field right ?
in this case the user enters a string and it will show a picture based on the string.

I will be using ImageSuckr

let's change the show method to this :

def text_field_strange_show(object, attribute)  
  suckr = ImageSuckr::GoogleSuckr.new
  url = suckr.get_image_url({"q" => object[attribute].to_s})
  link_to_inline_edit object, attribute, (object[attribute].nil? || object[attribute].empty?) ? "<i class='fi-plus'></i>".html_safe : image_tag(url, alt: object[attribute] )
end  

this is enough to have or awesome strange fiels working.

Go to your app directory the lawawy and edit your Gemfile
add :

gem 'image_suckr'  

save it

create a file named mk_strange.sh in your lawawy directory and paste this :

rails g inline_forms Category \  
    name:string \
  strange:text_field_strange \
    _enabled:yes \
    _presentation:'#{name}' \
    _order:name -f

in your shell run :

sh mk_strange.sh  

then this

rake db:migrate  

then this

bundle install  

and finally run your app

rails s  

point your browser to : 127.0.0.1/categories

and HOORAY !!

click on the plus sign and let's add something
yummy Macorani!

in the name fiels add : Yummy
and in the strange : macaroni

click OK

and click on Yummy
and there you have a delicious meal Bon apetit!