Class: Addable

Inherits:
Object
  • Object
show all
Defined in:
lib/aina/addable.rb

Constant Summary collapse

VALID_TYPES =
%w(text url email textarea datetime datetime-local radio checkbox select)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, fields = nil) ⇒ Addable

Returns a new instance of Addable.



6
7
8
9
10
11
12
# File 'lib/aina/addable.rb', line 6

def initialize(name, fields = nil)
	@type = 'post_type'
	@name = validate_name(name)
	@aina_version = Aina::VERSION

	parse_fields(fields)
end

Instance Attribute Details

#aina_versionObject

Returns the value of attribute aina_version.



2
3
4
# File 'lib/aina/addable.rb', line 2

def aina_version
  @aina_version
end

#fieldsObject

Returns the value of attribute fields.



2
3
4
# File 'lib/aina/addable.rb', line 2

def fields
  @fields
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/aina/addable.rb', line 2

def name
  @name
end

Class Method Details

.accepts?(type) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/aina/addable.rb', line 101

def self.accepts?(type)
	Generable.accepts?(type)
end

Instance Method Details

#add_custom_fieldsObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/aina/addable.rb', line 56

def add_custom_fields
	@file = Dir.pwd + "/post-types/#{@name}.php"
	
	# The post type wasn't using custom fields yet, 
	# so wrap them with the custom_fields function
	unless File.read(@file).include? "function #{@name}_custom_fields"
		File.open(@file, 'a+') {|file| file.puts custom_fields}
	else
		output = Array.new
		counter = 0 # This will help us keep track of lines

		File.open(@file).each do |line|
			output << line

			# When we reach a line that includes "function #{@name}_custom_fields"
			# it means that we have to add the items two lines later
			if line.include?("function #{@name}_custom_fields")
				# Negative number, so we know that this + 1 will we the line
				# after which we want to add text
				counter = -2
			end

			# Ok! It's ok to add the new items here, after 'return array('
			if counter === -1 and line.include?('return array(')
				output << fields_php_array
			end

			counter = counter + 1
		end

		File.open(@file, 'w') {|file| file.puts output}
	end

	# Make sure they can be saved
	text = File.read(@file)
	unless text.include? "function #{@name}_meta_box"
		template = File.read("#{Aina::TEMPLATES_DIR}/add.php")
		['{{name}}'].each do |replace|
			attribute = replace.gsub(/[{}]/, '')
			@output = template.gsub!(/#{replace}/, self.send(attribute))
		end
		File.open(@file, "a+") {|file| file.puts @output}
	end
end

#custom_fieldsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/aina/addable.rb', line 39

def custom_fields
	c = Array.new
	c << "/**"
	c << " * Custom data fields"
	c << " * Add these custom fields to the #{@name} post type"
	c << " * IMPORTANT: Thou shalt not rename this function, or bad things may happen"
	c << " */"
	c << "if ( ! function_exists( '#{@name}_custom_fields' ) ) {"
	c << " function #{@name}_custom_fields() {"
	c << "	 return array("
	c << "     #{fields_php_array}"
	c << "	 );"
	c << " }"
	c << "}"
	c.join("\n")
end

#fields_php_arrayObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/aina/addable.rb', line 27

def fields_php_array
	a = ''
	@fields.each do |f|
		a += "'#{f[:key]}' => array(\n"
		a += "  'label'     => '#{f[:key].capitalize}',\n"
		a += "  'type'      => '#{f[:type]}',\n"
		a += "  'options' 	=> array( 'option_1' => __('Option One'), 'option_2' => __('Option One') ),\n" if %w(radio checkbox select).include?(f[:type])
		a += "),\n"
	end
	a
end

#parse_fields(fields) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/aina/addable.rb', line 14

def parse_fields(fields)
	@fields = Array.new
	fields.each do |f|
		a = f.split(':')
		if ! a[1].nil? and VALID_TYPES.include? a[1]
			@fields << {key: a[0], type: a[1]}
		else
			raise Exception, "Type was missing for #{a[0]}" if a[1].nil?
			raise Exception, "#{a[1]} is not a valid type. Valid types are #{VALID_TYPES.join(', ')}"
		end
	end
end